[39] | 1 | #include "IRCLog.h" |
---|
| 2 | #include "Config/ConfigEnv.h" |
---|
| 3 | #include "IRCClient.h" |
---|
| 4 | #include <stdarg.h> |
---|
| 5 | |
---|
| 6 | IRCLog::IRCLog() |
---|
| 7 | { |
---|
| 8 | std::string logsDir = sConfig.GetStringDefault("LogsDir",""); |
---|
| 9 | std::string ircLogName = logsDir + "/IRC_"; |
---|
| 10 | std::string ircLogTimestamp = GetLogDateStr(); |
---|
| 11 | ircLogName += ircLogTimestamp +".log"; |
---|
| 12 | ircLogfile.open (ircLogName.c_str(), std::ios::app); |
---|
| 13 | } |
---|
| 14 | |
---|
| 15 | IRCLog::~IRCLog() |
---|
| 16 | { |
---|
| 17 | ircLogfile.close(); |
---|
| 18 | } |
---|
| 19 | // Was added because using the time for logs is very annoying... one log per day.. much cleaner looking.. |
---|
| 20 | std::string IRCLog::GetLogDateStr() const |
---|
| 21 | { |
---|
| 22 | time_t t = time(NULL); |
---|
| 23 | tm* aTm = localtime(&t); |
---|
| 24 | // YYYY year |
---|
| 25 | // MM month (2 digits 01-12) |
---|
| 26 | // DD day (2 digits 01-31) |
---|
| 27 | // HH hour (2 digits 00-23) |
---|
| 28 | // MM minutes (2 digits 00-59) |
---|
| 29 | // SS seconds (2 digits 00-59) |
---|
| 30 | char buf[20]; |
---|
| 31 | snprintf(buf,20,"%04d-%02d-%02d",aTm->tm_year+1900,aTm->tm_mon+1,aTm->tm_mday); |
---|
| 32 | return std::string(buf); |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | std::string IRCLog::GetLogDateTimeStr() const |
---|
| 36 | { |
---|
| 37 | time_t t = time(NULL); |
---|
| 38 | tm* aTm = localtime(&t); |
---|
| 39 | // YYYY year |
---|
| 40 | // MM month (2 digits 01-12) |
---|
| 41 | // DD day (2 digits 01-31) |
---|
| 42 | // HH hour (2 digits 00-23) |
---|
| 43 | // MM minutes (2 digits 00-59) |
---|
| 44 | // SS seconds (2 digits 00-59) |
---|
| 45 | char buf[30]; |
---|
| 46 | snprintf(buf,30,"[ %04d-%02d-%02d ] [ %02d:%02d:%02d ]",aTm->tm_year+1900,aTm->tm_mon+1,aTm->tm_mday,aTm->tm_hour,aTm->tm_min,aTm->tm_sec); |
---|
| 47 | return std::string(buf); |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | void IRCLog::WriteLog(const char *what, ...) |
---|
| 51 | { |
---|
| 52 | va_list ap; |
---|
| 53 | char tmpoutp[1024]; |
---|
| 54 | va_start(ap, what); |
---|
| 55 | vsnprintf(tmpoutp, 1024, what, ap ); |
---|
| 56 | va_end(ap); |
---|
| 57 | ircLogfile << tmpoutp; |
---|
| 58 | ircLogfile << "\n"; |
---|
| 59 | ircLogfile.flush(); |
---|
| 60 | } |
---|