├── example.cpp ├── readme.md └── minilogger.h /example.cpp: -------------------------------------------------------------------------------- 1 | #include "minilogger.h" 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | #define LOG(fmt, args...) do{\ 8 | char _buf[1024] = {0};\ 9 | snprintf(_buf, sizeof(_buf), "[%s:%s:%d][LOG_NORMAL]"fmt"\n",__FILE__,__FUNCTION__,__LINE__, ##args);\ 10 | mylog.Log(string(_buf));\ 11 | }while(false) 12 | 13 | 14 | 15 | //you can define your own Textdecorator and use it. 16 | Logger mylog("mylogfile.txt","this is title!",true,true); 17 | 18 | void myfunction(){ 19 | //test LOG in function 20 | // 21 | int a = 1; 22 | int b = 2; 23 | 24 | LOG("this is a log in function,a+b=%d",a+b); 25 | } 26 | 27 | 28 | //this is some example for using minilogger 29 | int main(){ 30 | //log with no args. 31 | mylog.Log("this is log."); 32 | std::cout << "do something" << std::endl; 33 | // log with args and function name and line number. 34 | LOG("test for LOG,args: number:[%d], string:[%s]",1,"yoyoyo"); 35 | 36 | myfunction(); 37 | std::cin.get(); 38 | return 0; 39 | 40 | } 41 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Minilogger 2 | 3 | Minilogger这是一个非常小巧的开源的c++日志工具,能实现在程序运行中随时将关键的事件通过log日志的方式保存在log文件中。 4 | 5 | 使用起来只需要在C++工程中将`minilogger.h`包含进来即可。 6 | 7 | Minilogger支持以下内容: 8 | 9 | - 自定义输出文件名称格式 10 | 11 | - 自定义总日志标题 12 | 13 | - 支持多个log文件按需要分类输出 14 | 15 | - 可以自定义log的格式和排版 16 | 17 | - 支持单日志的开启关闭时间 18 | 19 | - 支持每条log记录发生时间 20 | 21 | - 使用宏实现(具体详见example.cpp): 22 | 23 | - 支持输出log的代码位置和函数名称 24 | - 支持参数输出到日志 25 | 26 | 27 | 28 | # 使用 29 | 30 | 使用的时候只需要包含minilogger头文件到你的C++项目中就可以了, 31 | 32 | 使用示例: 33 | 34 | ```c++ 35 | #include "minilogger.h" 36 | 37 | int main(){ 38 | 39 | Logger mylog("mylogfile.txt","this is title!",true,true);//创建logger 40 | 41 | mylog.Log("this is log1.");//打log的方式和在控制台的log一样方便。 42 | 43 | return 0; 44 | } 45 | ``` 46 | 47 | 在输出的日志文件mylogfile.txt下: 48 | 49 | ``` 50 | [2018.05.23] [19:31:34] Session opened. 51 | [2018.05.23] [19:31:34] this is log1. 52 | [2018.05.23] [19:31:34] Session closed. 53 | ``` 54 | 55 | 解释: 56 | 57 | ```c++ 58 | Logger mylog("mylogfile.txt","this is title!",true,true); 59 | //创建logger,定义存储log的文件,如果文件不存在则自动创建,文件存在,往文件末尾追加log日志, 60 | //true,true为是否为每一条log附加时间,日期,可以关闭,置为false。 61 | //默认为日志的创建和结束追加时间日期 62 | //每个logger对象可以对指定一个日志文件进行操作,一个logger下的内容都会被放入同一个log文件和标题下 63 | //想要对不同事件分文件打log记录只需要多创建几个logger对象即可 64 | ``` 65 | 66 | logger是带一个装饰器的模板参数,默认的装饰器为TextDecorator 67 | 68 | 默认的TextDecorator装饰器,只是在每句话后加上换行,并对标题进行了横线分割, 69 | 70 | 用户也可以自定义装饰器得到自定义的日志类, 71 | 72 | 可以按照你自己的想法去扩展log装饰器TextDecorator类下的函数。 73 | 74 | 默认的装饰器TextDecorator: 75 | 76 | ```c++ 77 | class TextDecorator 78 | { 79 | public: 80 | static std::string FileHeader(const std::string& p_title) 81 | { 82 | return "==================================================\n" + 83 | p_title + "\n" + 84 | "==================================================\n\n"; 85 | } 86 | 87 | static std::string SessionOpen() 88 | { 89 | return "\n"; 90 | } 91 | 92 | static std::string SessionClose() 93 | { 94 | return "\n"; 95 | } 96 | 97 | static std::string Decorate(const std::string& p_string) 98 | { 99 | return p_string + "\n"; 100 | } 101 | }; 102 | 103 | ``` 104 | 105 | 106 | 107 | example中给出一种结合宏定义的方式输出: 108 | 109 | - log所处代码行数 110 | - log所处的函数名称 111 | - 打印参数 112 | 113 | ``` 114 | [2019.03.28] [20:13:11] [example.cpp:main:34][LOG_NORMAL]test for LOG,args: number:[1], string:[yoyoyo] 115 | [2019.03.28] [20:13:11] [example.cpp:myfunction:24][LOG_NORMAL]this is a log in function,a+b=3 116 | ``` 117 | 118 | 119 | 120 | # Minilogger 121 | 122 | Minilogger Is a single-head log library for C++ applications. It is very mini, highly scalable and can be configured according to user requirements. 123 | 124 | ## Quick Start 125 | 126 | ```c++ 127 | #include "minilogger.h" 128 | #include 129 | int main(){ 130 | Logger mylog("mylogfile.txt","this is title!",true,true);//creat logger 131 | mylog.Log("this is log1.");//log something 132 | mylog.Log("this is log2.");//log otherthing 133 | return 0; 134 | } 135 | ``` 136 | 137 | You can customize the format of the log through the text decorator 138 | 139 | ```c++ 140 | // ============================================================ 141 | // Here is an example of a simple log decorator, you can define your own decorator 142 | // ============================================================ 143 | class TextDecorator 144 | { 145 | public: 146 | static std::string FileHeader(const std::string& p_title) 147 | { 148 | return "==================================================\n" + 149 | p_title + "\n" + 150 | "==================================================\n\n"; 151 | } 152 | 153 | static std::string SessionOpen() 154 | { 155 | return "\n"; 156 | } 157 | 158 | static std::string SessionClose() 159 | { 160 | return "\n"; 161 | } 162 | 163 | static std::string Decorate(const std::string& p_string) 164 | { 165 | return p_string + "\n"; 166 | } 167 | }; 168 | 169 | ``` 170 | 171 | Easy-to-use log classes, called minilogger. 172 | 173 | Log example can log function name ,code line, and some args if you need. 174 | 175 | ``` 176 | [2019.03.28] [20:13:11] [example.cpp:main:34][LOG_NORMAL]test for LOG,args: number:[1], string:[yoyoyo] 177 | [2019.03.28] [20:13:11] [example.cpp:myfunction:24][LOG_NORMAL]this is a log in function,a+b=3 178 | ``` 179 | 180 | -------------------------------------------------------------------------------- /minilogger.h: -------------------------------------------------------------------------------- 1 | //Easy-to-use log classes,called minilogger 2 | #ifndef LOGGER_H 3 | #define LOGGER_H 4 | #include 5 | #include 6 | 7 | // ============================================================ 8 | // get system time 9 | // ============================================================ 10 | #include 11 | 12 | 13 | // ============================================================ 14 | // time in 24 hours hh:mm:ss format 15 | // ============================================================ 16 | static std::string TimeStamp() 17 | { 18 | char str[9]; 19 | 20 | // get the time, and convert it to struct tm format 21 | time_t a = time(0); 22 | struct tm* b = localtime(&a); 23 | 24 | // print the time to the string 25 | strftime(str, 9, "%H:%M:%S", b); 26 | 27 | return str; 28 | } 29 | 30 | // ============================================================ 31 | // date YYYY:MM:DD format 32 | // ============================================================ 33 | static std::string DateStamp() 34 | { 35 | char str[11]; 36 | 37 | // get the time, and convert it to struct tm format 38 | time_t a = time(0); 39 | struct tm* b = localtime(&a); 40 | 41 | // print the time to the string 42 | strftime(str, 11, "%Y.%m.%d", b); 43 | 44 | return str; 45 | } 46 | 47 | 48 | // ============================================================ 49 | // Here is an example of a simple log decorator, you can define your own decorator 50 | // ============================================================ 51 | class TextDecorator 52 | { 53 | public: 54 | static std::string FileHeader(const std::string& p_title) 55 | { 56 | return "==================================================\n" + 57 | p_title + "\n" + 58 | "==================================================\n\n"; 59 | } 60 | 61 | static std::string SessionOpen() 62 | { 63 | return "\n"; 64 | } 65 | 66 | static std::string SessionClose() 67 | { 68 | return "\n"; 69 | } 70 | 71 | static std::string Decorate(const std::string& p_string) 72 | { 73 | return p_string + "\n"; 74 | } 75 | }; 76 | 77 | 78 | // ============================================================ 79 | // New Logger with a new log file and new log title 80 | // ============================================================ 81 | template 82 | class Logger 83 | { 84 | public: 85 | Logger(const std::string& p_filename, 86 | const std::string& p_logtitle, 87 | bool p_timestamp = false, 88 | bool p_datestamp = false); 89 | 90 | ~Logger(); 91 | void Log(const std::string& p_entry); 92 | 93 | protected: 94 | std::fstream m_logfile; 95 | bool m_timestamp; 96 | bool m_datestamp; 97 | 98 | }; 99 | 100 | 101 | typedef Logger TextLog; 102 | 103 | 104 | 105 | template 106 | Logger::Logger(const std::string& p_filename, 107 | const std::string& p_logtitle, 108 | bool p_timestamp, 109 | bool p_datestamp) 110 | { 111 | // now the tricky part... testing to see if a file is open or not. 112 | // stupid C++. You need to open a file in read mode, and if it doesn't 113 | // open correctly, you know that it doesn't exist. 114 | // a file is open or not. 115 | std::fstream filetester(p_filename.c_str(), std::ios::in); 116 | 117 | if (filetester.is_open()) 118 | { 119 | // the file exists, so just close the test file 120 | filetester.close(); 121 | 122 | // open the real file and set app mode 123 | m_logfile.open(p_filename.c_str(), std::ios::out | std::ios::app); 124 | } 125 | else 126 | { 127 | // file doesn't exist. 128 | m_logfile.open(p_filename.c_str(), std::ios::out); 129 | 130 | // print out a file header to the file 131 | m_logfile << decorator::FileHeader(p_logtitle); 132 | } 133 | 134 | // print out an opening statement. Make sure it is time-and-date-stamped 135 | m_timestamp = true; 136 | m_datestamp = true; 137 | m_logfile << decorator::SessionOpen(); 138 | Log("Session opened."); 139 | m_timestamp = p_timestamp; 140 | m_datestamp = p_datestamp; 141 | 142 | } 143 | 144 | template< class decorator > 145 | Logger< decorator >::~Logger() 146 | { 147 | m_timestamp = true; 148 | m_datestamp = true; 149 | Log("Session closed."); 150 | m_logfile << decorator::SessionClose(); 151 | 152 | } 153 | 154 | 155 | template< class decorator > 156 | void Logger< decorator >::Log(const std::string& p_entry) 157 | { 158 | std::string message; 159 | 160 | if (m_datestamp) 161 | { 162 | message += "[" + DateStamp() + "] "; 163 | } 164 | if (m_timestamp) 165 | { 166 | message += "[" + TimeStamp() + "] "; 167 | } 168 | 169 | message += p_entry; 170 | m_logfile << decorator::Decorate(message); 171 | m_logfile.flush(); 172 | } 173 | 174 | 175 | 176 | 177 | 178 | #endif 179 | --------------------------------------------------------------------------------