simplelogger.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. #include "simplelogger.hpp"
  2. // Generates filename (in format prefix-log-data-time)
  3. std::string SimpleLogger::generateFileName()
  4. {
  5. std::chrono::time_point<std::chrono::system_clock> time_now = std::chrono::system_clock::now();
  6. std::time_t t_n = std::chrono::system_clock::to_time_t(time_now);
  7. char logfile_creation_time[100];
  8. std::strftime(logfile_creation_time, sizeof(logfile_creation_time), "%Y%m%d-%H%M%S", std::localtime(&t_n));
  9. std::string logname = this->log_name_prefix + "-log-" + std::string(logfile_creation_time) + ".log";
  10. std::cout << logname << std::endl;
  11. return logname;
  12. }
  13. // Generates prefix (in format of [TIME] / [FLAG]) for log string
  14. std::string SimpleLogger::generateLogString(int flag = INFO)
  15. {
  16. auto time = std::chrono::system_clock::now(); // get the current time
  17. std::time_t t = std::chrono::system_clock::to_time_t(time);
  18. char log_time[100];
  19. std::strftime(log_time, sizeof(log_time), "%Y-%m-%d %H:%M:%S", std::localtime(&t));
  20. auto since_epoch = time.time_since_epoch(); // get the duration since epoch
  21. auto time_millis = std::chrono::duration_cast<std::chrono::milliseconds>(since_epoch);
  22. auto time_seconds = std::chrono::duration_cast<std::chrono::seconds>(since_epoch);
  23. unsigned long long millis = time_millis.count() - 1000*time_seconds.count();
  24. std::string logstring = std::string("[") + std::string(log_time) + std::string(":") + std::to_string(millis) + std::string("]");
  25. // Different flags
  26. switch(flag)
  27. {
  28. case ERROR:
  29. logstring = logstring + " / [ERROR]\t";
  30. break;
  31. case DEBUG_ERROR:
  32. logstring = logstring + " / [ERROR]\t";
  33. break;
  34. case DONE:
  35. logstring = logstring + " / [DONE]\t";
  36. break;
  37. case WARN:
  38. logstring = logstring + " / [WARN]\t";
  39. break;
  40. case INFO:
  41. logstring = logstring + " / [INFO]\t";
  42. break;
  43. case OK:
  44. logstring = logstring + " / [OK]\t";
  45. break;
  46. case DEBUG:
  47. logstring = logstring + " / [DEBUG]\t";
  48. break;
  49. default:
  50. logstring = logstring + " / [UNDEF]\t";
  51. break;
  52. }
  53. return logstring;
  54. }
  55. // Main Constructors (we need to send reference to our main log stream)
  56. SimpleLogger::SimpleLogger(std::ostream& ls = std::cout, std::string prefix = "", std::string dir = "") : log_stream(ls)
  57. {
  58. this->initializeLogger(dir, prefix);
  59. }
  60. SimpleLogger::SimpleLogger(std::ostream& ls = std::cout) : log_stream(ls)
  61. {
  62. this->initializeLogger("", "some");
  63. }
  64. SimpleLogger::SimpleLogger(std::ostream& ls, std::string prefix) : log_stream(ls)
  65. {
  66. this->initializeLogger("", prefix);
  67. }
  68. // Initialize function
  69. void SimpleLogger::initializeLogger(std::string dir = "", std::string prefix = "")
  70. {
  71. this->log_flag.flag = INFO;
  72. if(dir != "")
  73. {
  74. #ifdef _WIN32
  75. CreateDirectoryA(dir.c_str(), NULL);
  76. this->log_name_prefix = dir + std::string("\\") + prefix;
  77. #endif // _WIN32
  78. #if defined(_LINUX)
  79. mkdir(dir.c_str(), 0777);
  80. this->log_name_prefix = dir + std::string("/") +prefix;
  81. #endif // defined
  82. }
  83. else
  84. {
  85. this->log_name_prefix = prefix;
  86. }
  87. this->log_string = "";
  88. this->console_enabled = false;
  89. this->log_name = generateFileName();
  90. this->file_stream.open(this->log_name);
  91. this->log_stream.rdbuf(this->file_stream.rdbuf());
  92. }
  93. // Copy logger constructor
  94. SimpleLogger::SimpleLogger(const SimpleLogger& other) : log_stream(other.log_stream)
  95. {
  96. this->log_flag = other.log_flag;
  97. this->log_name_prefix = other.log_name_prefix;
  98. this->log_string = other.log_string;
  99. this->console_enabled = other.console_enabled;
  100. this->log_name = other.log_name;
  101. }
  102. // Close logger stream
  103. void SimpleLogger::closeLogger()
  104. {
  105. this->file_stream.close();
  106. }
  107. // Destructor
  108. SimpleLogger::~SimpleLogger()
  109. {
  110. this->closeLogger();
  111. }
  112. // Setting logger flag
  113. void SimpleLogger::setLoggerFlag(LogPref::Flag log_flag = LogPref::Flag(INFO))
  114. {
  115. this->log_flag = log_flag;
  116. }
  117. // Setting ability to console output
  118. void SimpleLogger::enableConsoleOutput(bool enable_console)
  119. {
  120. console_enabled = enable_console;
  121. }
  122. // Sets flag just for one record;
  123. SimpleLogger SimpleLogger::operator<<(LogPref::Flag flag)
  124. {
  125. SimpleLogger s(*this);
  126. s.setLoggerFlag(flag);
  127. return s;
  128. }
  129. // Shift operators (follow standard ostream shift operators)
  130. SimpleLogger SimpleLogger::operator<<(bool val){
  131. // Firstly printing in console
  132. if(console_enabled){
  133. std::cout << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << val;
  134. }
  135. // Printing in file
  136. log_stream << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << val;
  137. return *this << LogPref::Flag(NO_LOG_STRING);
  138. }
  139. SimpleLogger SimpleLogger::operator<<(long val) {
  140. if (console_enabled) {
  141. std::cout << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << val;
  142. }
  143. log_stream << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << val;
  144. return *this << LogPref::Flag(NO_LOG_STRING);
  145. }
  146. SimpleLogger SimpleLogger::operator<<(unsigned long val) {
  147. if (console_enabled) {
  148. std::cout << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << val;
  149. }
  150. log_stream << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << val;
  151. return *this << LogPref::Flag(NO_LOG_STRING);
  152. }
  153. SimpleLogger SimpleLogger::operator<<(long long val) {
  154. if (console_enabled) {
  155. std::cout << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << val;
  156. }
  157. log_stream << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << val;
  158. return *this << LogPref::Flag(NO_LOG_STRING);
  159. }
  160. SimpleLogger SimpleLogger::operator<<(unsigned long long val) {
  161. if (console_enabled) {
  162. std::cout << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << val;
  163. }
  164. log_stream << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << val;
  165. return *this << LogPref::Flag(NO_LOG_STRING);
  166. }
  167. SimpleLogger SimpleLogger::operator<<(double val) {
  168. if (console_enabled) {
  169. std::cout << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << val;
  170. }
  171. log_stream << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << val;
  172. return *this << LogPref::Flag(NO_LOG_STRING);
  173. }
  174. SimpleLogger SimpleLogger::operator<<(long double val) {
  175. if (console_enabled) {
  176. std::cout << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << val;
  177. }
  178. log_stream << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << val;
  179. return *this << LogPref::Flag(NO_LOG_STRING);
  180. }
  181. SimpleLogger SimpleLogger::operator<<(const void* val) {
  182. if (console_enabled) {
  183. std::cout << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << val;
  184. }
  185. log_stream << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << val;
  186. return *this << LogPref::Flag(NO_LOG_STRING);
  187. }
  188. SimpleLogger SimpleLogger::operator<<(std::nullptr_t) {
  189. if (console_enabled) {
  190. std::cout << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << "nullptr";
  191. }
  192. log_stream << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << "nullptr";
  193. return *this << LogPref::Flag(NO_LOG_STRING);
  194. }
  195. SimpleLogger SimpleLogger::operator<<(short val) {
  196. if (console_enabled) {
  197. std::cout << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << val;
  198. }
  199. log_stream << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << val;
  200. return *this << LogPref::Flag(NO_LOG_STRING);
  201. }
  202. SimpleLogger SimpleLogger::operator<<(int val) {
  203. if (console_enabled) {
  204. std::cout << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << val;
  205. }
  206. log_stream << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << val;
  207. return *this << LogPref::Flag(NO_LOG_STRING);
  208. }
  209. SimpleLogger SimpleLogger::operator<<(unsigned short val) {
  210. if (console_enabled) {
  211. std::cout << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << val;
  212. }
  213. log_stream << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << val;
  214. return *this << LogPref::Flag(NO_LOG_STRING);
  215. }
  216. SimpleLogger SimpleLogger::operator<<(unsigned int val) {
  217. if (console_enabled) {
  218. std::cout << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << val;
  219. }
  220. log_stream << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << val;
  221. return *this << LogPref::Flag(NO_LOG_STRING);
  222. }
  223. SimpleLogger SimpleLogger::operator<<(float val) {
  224. if (console_enabled) {
  225. std::cout << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << val;
  226. }
  227. log_stream << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << val;
  228. return *this << LogPref::Flag(NO_LOG_STRING);
  229. }
  230. SimpleLogger SimpleLogger::operator<<(std::streambuf *sb) {
  231. if (console_enabled) {
  232. std::cout << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << sb;
  233. }
  234. log_stream << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << sb;;
  235. return *this << LogPref::Flag(NO_LOG_STRING);
  236. }
  237. SimpleLogger SimpleLogger::operator<<(std::ios_base& (*func)(std::ios_base&)) {
  238. if (console_enabled) {
  239. std::cout << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << func;
  240. }
  241. log_stream << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << func;
  242. return *this << LogPref::Flag(NO_LOG_STRING);
  243. }
  244. SimpleLogger SimpleLogger::operator<<(std::ostream& (*func)(std::ostream&)) {
  245. if (console_enabled) {
  246. std::cout << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << func;
  247. }
  248. log_stream << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << func;
  249. return *this << LogPref::Flag(NO_LOG_STRING);
  250. }
  251. SimpleLogger SimpleLogger::operator<<(std::_Setfill<char> func)
  252. {
  253. if (console_enabled) {
  254. std::cout << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << func;
  255. }
  256. log_stream << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << func;
  257. return *this << LogPref::Flag(NO_LOG_STRING);
  258. }
  259. SimpleLogger SimpleLogger::operator<<(std::_Setw func)
  260. {
  261. if (console_enabled) {
  262. std::cout << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << func;
  263. }
  264. log_stream << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << func;
  265. return *this << LogPref::Flag(NO_LOG_STRING);
  266. }
  267. SimpleLogger SimpleLogger::operator<<(std::string val) {
  268. if (console_enabled) {
  269. std::cout << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << val;
  270. }
  271. log_stream << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << val;
  272. return *this << LogPref::Flag(NO_LOG_STRING);
  273. }
  274. SimpleLogger SimpleLogger::operator<<(const char* val) {
  275. if (console_enabled) {
  276. std::cout << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << val;
  277. }
  278. log_stream << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << val;
  279. return *this << LogPref::Flag(NO_LOG_STRING);
  280. }
  281. SimpleLogger SimpleLogger::operator<<(const signed char* val) {
  282. if (console_enabled) {
  283. std::cout << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << val;
  284. }
  285. log_stream << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << val;
  286. return *this << LogPref::Flag(NO_LOG_STRING);
  287. }
  288. SimpleLogger SimpleLogger::operator<<(const unsigned char* val) {
  289. if (console_enabled) {
  290. std::cout << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << val;
  291. }
  292. log_stream << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << val;
  293. return *this << LogPref::Flag(NO_LOG_STRING);
  294. }
  295. SimpleLogger SimpleLogger::operator<<(const char val) {
  296. if (console_enabled) {
  297. std::cout << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << val;
  298. }
  299. log_stream << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << val;
  300. return *this << LogPref::Flag(NO_LOG_STRING);
  301. }
  302. SimpleLogger SimpleLogger::operator<<(const unsigned char val) {
  303. if (console_enabled) {
  304. std::cout << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << val;
  305. }
  306. log_stream << (this->log_flag.flag == NO_LOG_STRING ? "" : generateLogString(this->log_flag.flag)) << val;
  307. return *this << LogPref::Flag(NO_LOG_STRING);
  308. }