TinyLlama.cpp 1.0
A lightweight C++ implementation of the TinyLlama language model
Loading...
Searching...
No Matches
logger.h
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4#include <vector>
5#include <fstream>
6#include <sstream>
7#include <iomanip>
8
9// Windows header protection
10#ifdef _WIN32
11#ifndef NOMINMAX
12#define NOMINMAX
13#endif
14#ifndef WIN32_LEAN_AND_MEAN
15#define WIN32_LEAN_AND_MEAN
16#endif
17// Undefine common Windows macros that might conflict
18#ifdef ERROR
19#undef ERROR
20#endif
21#ifdef DEBUG
22#undef DEBUG
23#endif
24#ifdef INFO
25#undef INFO
26#endif
27#ifdef WARNING
28#undef WARNING
29#endif
30#ifdef CRITICAL
31#undef CRITICAL
32#endif
33#endif // _WIN32
34
51class Logger {
52
53 public:
54 // Explicitly scoped enumeration to avoid conflicts with Windows macros
55 enum class Level : int {
56 DEBUG = 0,
57 INFO = 1,
58 WARNING = 2,
59 ERROR = 3,
60 CRITICAL = 4,
61 OFF = 5
62 };
63
64 static void set_level(Level new_level);
65 static Level get_level();
66 static void set_logfile(const std::string& filename);
67 static void enable_console(bool enabled);
68
69 static void debug(const std::string& message);
70 static void info(const std::string& message);
71 static void warning(const std::string& message);
72 static void error(const std::string& message);
73 static void critical(const std::string& message);
74 static void fatal(const std::string& message);
75
76 // Helper to convert pointer to string for logging
77 static std::string ptrToString(const void* ptr);
78
79 // Helper to convert uint16_t to hex string for logging
80 static std::string uint16ToHex(uint16_t val);
81
82 // Templated helper to convert unsigned integral types to hex string
83 template <typename T>
84 static std::string to_hex(T val);
85
86 // Helper for vector stats (if it's part of public API, otherwise private)
87 static void log_vector_stats(const std::string& name, const std::vector<float>& v, int n_show = 5);
88 static void log_vector_stats_int8(const std::string& name, const std::vector<int8_t>& v, int n_show = 5);
89
90 private:
92 static std::ofstream log_file_stream_;
93 static std::string log_file_path_;
94 static bool console_enabled_;
96
97 static void log_internal(Level level, const std::string& message);
98 static std::string level_to_string(Level level);
100};
Static logging class for application-wide logging.
Definition logger.h:51
static void log_vector_stats(const std::string &name, const std::vector< float > &v, int n_show=5)
Definition logger.cpp:160
static bool log_file_truncated_
Definition logger.h:95
static std::string log_file_path_
Definition logger.h:93
static void debug(const std::string &message)
Definition logger.cpp:131
static std::string to_hex(T val)
Definition logger.cpp:233
static void log_internal(Level level, const std::string &message)
Definition logger.cpp:101
static void warning(const std::string &message)
Definition logger.cpp:139
static std::string ptrToString(const void *ptr)
Definition logger.cpp:225
static void ensure_logfile_open_and_truncated()
Definition logger.cpp:71
static bool console_enabled_
Definition logger.h:94
static void set_level(Level new_level)
Definition logger.cpp:34
static void enable_console(bool enabled)
Definition logger.cpp:54
static std::string level_to_string(Level level)
Definition logger.cpp:59
static Level current_level_
Definition logger.h:91
static Level get_level()
Definition logger.cpp:39
Level
Definition logger.h:55
static std::string uint16ToHex(uint16_t val)
Definition logger.cpp:242
static std::ofstream log_file_stream_
Definition logger.h:92
static void info(const std::string &message)
Definition logger.cpp:135
static void error(const std::string &message)
Definition logger.cpp:143
static void fatal(const std::string &message)
Definition logger.cpp:151
static void log_vector_stats_int8(const std::string &name, const std::vector< int8_t > &v, int n_show=5)
Definition logger.cpp:195
static void critical(const std::string &message)
Definition logger.cpp:147
static void set_logfile(const std::string &filename)
Definition logger.cpp:44