diff options
Diffstat (limited to 'src/util/consoleLogger.cpp')
| -rw-r--r-- | src/util/consoleLogger.cpp | 355 |
1 files changed, 355 insertions, 0 deletions
diff --git a/src/util/consoleLogger.cpp b/src/util/consoleLogger.cpp new file mode 100644 index 0000000..b523b40 --- /dev/null +++ b/src/util/consoleLogger.cpp @@ -0,0 +1,355 @@ +/* + # Copyright (c) 2009, 2010 - OpenSLX Project, Computer Center University of + # Freiburg + # + # This program is free software distributed under the GPL version 2. + # See http://openslx.org/COPYING + # + # If you have any feedback please consult http://openslx.org/feedback and + # send your suggestions, praise, or complaints to feedback@openslx.org + # + # General information about OpenSLX can be found at http://openslx.org/ + # ----------------------------------------------------------------------------- + # consoleLogger.cpp + # - ???. + # ----------------------------------------------------------------------------- + */ + +#include "consoleLogger.h" + +#ifdef never +template<class T> +void ConsoleLogger::addListener(T* who, void (T :: *func)(QString)) +{ + LogNotify<T> *test = new LogNotify<T>(who, func); + _CBLog.push_back(dynamic_cast<LogNotifyEntry*>(test)); +} + +template<class T> +void ConsoleLogger::removeListener(T* who, void (T :: *func)(QString)) +{ + if (_CBLog.size()) + { + for (std::list<LogNotifyEntry*>::iterator it = _CBLog.begin(); it != _CBLog.end(); it++) + { + LogNotify<T> *probe = dynamic_cast<LogNotify<T>*>((*it)); + if (probe) + { + if (probe->getCallee() == who && probe->getCB() == func) + { + _CBLog.remove((*it)); + } + } + } + } +// LogNotify<T> *test = new LogNotify<T>(who, func); + // _CBLog.push_back(dynamic_cast<LogNotifyEntry*>(test)); +} + +#endif +int ConsoleLogger::writeLine(QString message, LOG_LEVEL level) +{ + if (message.size() == 0) + return -1; + + ConsoleEntry consoleEntry(_getTimeStamp(), message, level); + _announceWrite(consoleEntry); // tell the listeners! + _writeLine2File(consoleEntry.getLine()); + _log.push_back(consoleEntry); + + return _log.size(); +} + +int ConsoleLogger::writeline(const char* message, LOG_LEVEL level) +{ + if (message) + { + if (strlen(message) > 0) + return writeLine(QString(message), level); + } + return -1; +} + +int ConsoleLogger::writeError(QString error) +{ + return writeLine(error, LOG_ERROR); +} + +int ConsoleLogger::writeError(const char* error) +{ + return writeLine(error, LOG_ERROR); +} + +int ConsoleLogger::writeTerminal(QString terminal) +{ + // printing the line on the console happens further down the line! + return writeLine(terminal, LOG_TERMINAL); +} + +int ConsoleLogger::writeTerminal(const char* terminal) +{ + if (terminal) + if (strlen(terminal) > 0) + return writeTerminal(QString(terminal)); + + terminal = NULL; + return -1; +} + +int ConsoleLogger::writeNetwork(QString network) +{ + // printing the line on the console happens further down the line! + return writeLine(network, LOG_NETWORK); +} + +int ConsoleLogger::writeNetwork(const char* network) +{ + if (network) + if (strlen(network) > 0) + return writeNetwork(QString(network)); + + network = NULL; + return -1; +} + +int ConsoleLogger::writeChat(QString chat) +{ + return writeLine(chat, LOG_CHAT); +} + +int ConsoleLogger::writeChat(const char* chat) +{ + if (chat) + if (strlen(chat) > 0) + return writeChat(QString(chat)); + + chat = NULL; + return -1; +} + +void ConsoleLogger::getLine(int lineNum, char* line) +{ + QString tmp; + getLine(lineNum, &tmp); + if (tmp.size() > 0) + { + line = new char[_log[lineNum].getLine().size()+1]; + if (line) + { + memcpy(line, _log[lineNum].getLine().toUtf8().data(), _log[lineNum].getLine().size()); + if (strlen(line) > 0) + return; + } + } + line = NULL; + +} + +void ConsoleLogger::getLine(int lineNum, QString* line) +{ + if (lineNum <= int(_log.size())) + { + if (lineNum > 0) + lineNum--; + else if (lineNum < 0) + { + line = NULL; + return; + } + } + else + { + line->clear(); + line = NULL; + } +} + +void ConsoleLogger::setLogPath(QString logPath) +{ + getLogger()->_logPath = logPath; + getLogger()->_prepareLog(); +} + +void ConsoleLogger::setLogName(QString logName) +{ + getLogger()->_logName = logName; + getLogger()->_prepareLog(); +} + +void ConsoleLogger::dumpLog(DUMP_MODE mode) +{ + if (mode == DUMP_FILE || (DUMP_FILE_CONCAT && !_fileRead)) + getLogger()->_writeLog(); + if (mode == DUMP_FILE_CONCAT && _fileRead) + getLogger()->_writeLog(true); + if (mode == DUMP_ALL_LISTENERS) + { + for (int i = 0; i <int(_log.size()); i++) + { + _announceWrite(_log[i]); + } + } +} + +int ConsoleLogger::count = 0; + +ConsoleLogger* ConsoleLogger::_logger; + +ConsoleLogger* ConsoleLogger::getLogger() +{ + if (!_logger) + { + return ConsoleLogger::_logger = new ConsoleLogger(); + } + else + return ConsoleLogger::_logger; +} + +ConsoleLogger::ConsoleLogger() + : _logName(""), + _logPath(getPolicyFilePath(QString(""))), + _fileRead(false), + entryDispatcher() +{ +// _prepareLog(); +} + +ConsoleLogger::~ConsoleLogger() +{ + // doesnt work!? + _writeLog(); +} + +void ConsoleLogger::_prepareLog() +{ + _logFile.close(); + _logFileGood = false; + _readLog(); + + mkdir(getPolicyFilePath(QString()).toUtf8().data(), 0777); + QString fullpath; + fullpath.append(_logPath); + //TODO: handle wether path/ or path were entered? + fullpath.append(_logName); + _logFile.open(fullpath.toUtf8().data(), std::ofstream::out | std::ofstream::app); + if (_logFile.good()) + { + _logFileGood = true; + writeTerminal(QString("LogPath/Name changed to: ").append(fullpath)); + } + else + { + printf("ERROR: Logfile ( %s ) not accessible/found. Logs will not be available.\n", _logPath.toUtf8().data()); + } + _logFile.close(); +} + +void ConsoleLogger::_writeLine2File(QString line) +{ + if (_logFileGood) + { + QString fullpath; + fullpath.append(_logPath); + //TODO: handle wether path/ or path were entered? + fullpath.append(_logName); + _logFile.open(fullpath.toUtf8().data(), std::ofstream::out | std::ofstream::app); + + if (_logFile.good()) // one can never be too sure + { + _logFile.write(line.toUtf8().data(), line.size()); + } + + _logFile.close(); + } + +} + +// overwrites the file and dumps the complete log archive of this session +void ConsoleLogger::_writeLog(bool concat) +{ + if (_logFileGood) + { + QString fullpath; + fullpath.append(_logPath); + //TODO: handle wether path/ or path were entered? + fullpath.append(_logName); + + std::vector<ConsoleEntry> *tmpLog = NULL; + + if (concat) + { + std::vector<ConsoleEntry> newLog = _prev_log; + newLog.insert(newLog.end(), _log.begin(), _log.end()); + tmpLog = &newLog; + } + else + { + tmpLog = &_log; + } + _logFile.open(fullpath.toUtf8().data(), std::ofstream::out | std::ofstream::trunc); + + if (_logFile.good()) // one can never be too sure + { + for (int i = 0; i < int(tmpLog->size()); i++) + { + _logFile.write(tmpLog->at(i).getLine().toUtf8().data(), tmpLog->at(i).getLine().size()); + } + } + + _logFile.close(); + } +} + +void ConsoleLogger::_announceWrite(ConsoleEntry consoleEntry) +{ + + entryDispatcher.fire(consoleEntry); +} + +void ConsoleLogger::_readLog(QString path) +{ + // todo, read the file, filter the entries and recombine them if possible etc.. + // then save it all to _log_prev. + // then set _readFile to true, so the dump will concat our current log to the old and THEN overwrite the whole file + _prev_log.clear(); + _fileRead = false; +} + +QString ConsoleLogger::_getTimeStamp() +{ + time_t rawtime; + tm * ptm; + time ( &rawtime ); + + //ptm = gmtime ( &rawtime ); + ptm = localtime ( &rawtime ); + + QString tmpStr; + if (ptm->tm_mday < 10) + tmpStr.append(QString("0")); + tmpStr.append(int2String(ptm->tm_mday)); + tmpStr.push_back('.'); + if (ptm->tm_mon < 10) + tmpStr.append(QString("0")); + tmpStr.append(int2String(int(ptm->tm_mon) +1)); + tmpStr.push_back('.'); + int year = (ptm->tm_year % 100); + if (year < 10) + tmpStr.append(QString("0")); + tmpStr.append(int2String(year)); + tmpStr.push_back(' '); + if (ptm->tm_hour < 10) + tmpStr.append(QString("0")); + tmpStr.append(int2String(ptm->tm_hour)); + tmpStr.push_back(':'); + if (ptm->tm_min < 10) + tmpStr.append(QString("0")); + tmpStr.append(int2String(ptm->tm_min)); + tmpStr.push_back(':'); + if (ptm->tm_sec < 10) + tmpStr.append(QString("0")); + tmpStr.append(int2String(ptm->tm_sec)); + + return tmpStr; +} + |
