17void ensure_log_truncated() {
18 static bool truncated =
false;
20 std::ofstream log(
"debugging.log", std::ios::trunc);
67 default:
return "UNKNOWN";
85 std::cerr <<
"[LOGGER_ERROR] Failed to open log file: " <<
log_file_path_ << std::endl;
96 std::cerr <<
"[LOGGER_ERROR] Failed to re-open/truncate log file: " <<
log_file_path_ << std::endl;
109 auto now = std::chrono::system_clock::now();
110 auto now_c = std::chrono::system_clock::to_time_t(now);
112 std::ostringstream log_line_stream;
113 log_line_stream << std::put_time(std::localtime(&now_c),
"%Y-%m-%d %H:%M:%S")
115 std::string log_line = log_line_stream.str();
119 std::cerr << log_line << std::endl;
121 std::cout << log_line << std::endl;
156 std::cerr <<
"[FATAL] " << message << std::endl;
157 std::exit(EXIT_FAILURE);
161 const std::vector<float>& v,
int n_show) {
166 float min_val = v[0];
167 float max_val = v[0];
168 double sum_val = 0.0;
169 bool all_finite_vals =
true;
171 for (
float val : v) {
172 if (std::isnan(val) || std::isinf(val)) {
173 all_finite_vals =
false;
175 if (val < min_val) min_val = val;
176 if (val > max_val) max_val = val;
177 sum_val +=
static_cast<double>(val);
179 float mean_val =
static_cast<float>(sum_val / v.size());
181 std::ostringstream oss;
182 oss << name <<
": size=" << v.size()
183 <<
", min=" << min_val
184 <<
", max=" << max_val
185 <<
", mean=" << mean_val
186 <<
", all_finite=" << (all_finite_vals ?
"yes" :
"no")
188 for (
int i = 0; i < std::min((
int)v.size(), n_show); ++i) {
189 oss << v[i] << (i == std::min((
int)v.size(), n_show) - 1 ?
"" :
", ");
200 int8_t min_val = v[0];
201 int8_t max_val = v[0];
202 long long sum_val = 0;
204 for (int8_t val : v) {
205 if (val < min_val) min_val = val;
206 if (val > max_val) max_val = val;
207 sum_val +=
static_cast<long long>(val);
210 float mean_val =
static_cast<float>(
static_cast<double>(sum_val) / v.size());
212 std::ostringstream oss;
213 oss << name <<
": size=" << v.size()
214 <<
", min=" <<
static_cast<int>(min_val)
215 <<
", max=" <<
static_cast<int>(max_val)
216 <<
", mean=" << mean_val
218 for (
int i = 0; i < std::min((
int)v.size(), n_show); ++i) {
219 oss << static_cast<int>(v[i]) << (i == std::min((
int)v.size(), n_show) - 1 ?
"" :
", ");
226 std::ostringstream oss;
234 std::ostringstream oss;
235 oss <<
"0x" << std::hex << static_cast<unsigned long long>(val);
static void log_vector_stats(const std::string &name, const std::vector< float > &v, int n_show=5)
static bool log_file_truncated_
static std::string log_file_path_
static void debug(const std::string &message)
static std::string to_hex(T val)
static void log_internal(Level level, const std::string &message)
static void warning(const std::string &message)
static std::string ptrToString(const void *ptr)
static void ensure_logfile_open_and_truncated()
static bool console_enabled_
static void set_level(Level new_level)
static void enable_console(bool enabled)
static std::string level_to_string(Level level)
static Level current_level_
static std::string uint16ToHex(uint16_t val)
static std::ofstream log_file_stream_
static void info(const std::string &message)
static void error(const std::string &message)
static void fatal(const std::string &message)
static void log_vector_stats_int8(const std::string &name, const std::vector< int8_t > &v, int n_show=5)
static void critical(const std::string &message)
static void set_logfile(const std::string &filename)
Logging utilities for the TinyLlama implementation.