]> git.stg.codes - ssmd.git/blob - src/logger.cpp
Removed GTS string.
[ssmd.git] / src / logger.cpp
1 #include <ctime>
2
3 #include "logger.h"
4
5 using SSMD::Logger;
6
7 bool Logger::setLogFile(const std::string & fileName)
8 {
9     fout.open(fileName.c_str(), std::ios::app);
10     logFile = fileName;
11     return !(consoleLog = !fout.is_open());
12 }
13
14 std::ostream & Logger::operator<<(const std::string & str)
15 {
16     _logDate();
17     if (consoleLog) {
18         return std::cout << str;
19     } else {
20         fout.close();
21         fout.open(logFile.c_str(), std::ios::app);
22         if (fout) {
23             fout << str;
24         }
25         return fout;
26     }
27 }
28
29 inline
30 void Logger::_logDate()
31 {
32     time_t t = time(NULL);
33     struct tm *ts = localtime(&t);
34     if (consoleLog) {
35         std::cout << "["
36                   << (ts->tm_year + 1900) << "-"
37                   << (ts->tm_mon < 9 ? "0" : "") << (ts->tm_mon + 1) << "-"
38                   << (ts->tm_mday < 10 ? "0" : "") << ts->tm_mday << " "
39                   << (ts->tm_hour < 10 ? "0" : "") << ts->tm_hour << ":"
40                   << (ts->tm_min < 10 ? "0" : "") << ts->tm_min << ":"
41                   << (ts->tm_sec < 10 ? "0" : "") << ts->tm_sec
42                   << "] ";
43     } else {
44         fout.close();
45         fout.open(logFile.c_str(), std::ios::app);
46         if (fout) {
47             fout << "["
48                  << (ts->tm_year + 1900) << "-"
49                  << (ts->tm_mon < 9 ? "0" : "") << (ts->tm_mon + 1) << "-"
50                  << (ts->tm_mday < 10 ? "0" : "") << ts->tm_mday << " "
51                  << (ts->tm_hour < 10 ? "0" : "") << ts->tm_hour << ":"
52                  << (ts->tm_min < 10 ? "0" : "") << ts->tm_min << ":"
53                  << (ts->tm_sec < 10 ? "0" : "") << ts->tm_sec
54                  << "] ";
55         }
56     }
57 }