diff options
author | Matthias Schiffer <matthias@gamezock.de> | 2008-09-21 19:58:55 +0200 |
---|---|---|
committer | Matthias Schiffer <matthias@gamezock.de> | 2008-09-21 19:58:55 +0200 |
commit | b16c75d98c57b8f1efa26ab6eae35431c04c4c1e (patch) | |
tree | 4348ceac0e673a1742565cf0f291a8d540774668 | |
parent | 84f69d7aacef531c0ad809d7511c7ab7bce7469c (diff) | |
download | mad-b16c75d98c57b8f1efa26ab6eae35431c04c4c1e.tar mad-b16c75d98c57b8f1efa26ab6eae35431c04c4c1e.zip |
Printf-artige Funktion logf zum Logger hinzugef?gt
-rw-r--r-- | src/Client/CommandParser.cpp | 22 | ||||
-rw-r--r-- | src/Common/Exception.cpp | 2 | ||||
-rw-r--r-- | src/Common/Logger.cpp | 46 | ||||
-rw-r--r-- | src/Common/Logger.h | 6 | ||||
-rw-r--r-- | src/Core/ConnectionManager.cpp | 2 | ||||
-rw-r--r-- | src/mad.cpp | 2 | ||||
-rw-r--r-- | src/madc.cpp | 2 |
7 files changed, 67 insertions, 15 deletions
diff --git a/src/Client/CommandParser.cpp b/src/Client/CommandParser.cpp index ccf5a74..87202b4 100644 --- a/src/Client/CommandParser.cpp +++ b/src/Client/CommandParser.cpp @@ -59,7 +59,7 @@ void CommandParser::printUsage(const std::string& command) { const CommandParser::Command *cmd = findCommand(command); if(cmd) - Common::Logger::log(std::string("Usage: ") + cmd->cmdline + "\n"); + Common::Logger::logf("Usage: %s\n", cmd->cmdline); } void CommandParser::printHostStatus(const Net::Packets::HostStatusPacket &packet) { @@ -135,10 +135,10 @@ void CommandParser::helpCommand(const std::vector<std::string> &args) { std::cout << command->longdesc << std::endl << std::endl; } else - Common::Logger::log(Common::Logger::WARNING, args[0] + ": Command '" + args[1] + "' doesn't exist.\n"); + Common::Logger::logf(Common::Logger::WARNING,"%s: Command '%s' doesn't exist.\n", args[0].c_str(), args[1].c_str()); } else { - Common::Logger::log(Common::Logger::ERROR, args[0] + ": Too many arguments."); + Common::Logger::logf(Common::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); printUsage("help"); } } @@ -154,7 +154,7 @@ void CommandParser::listHostsCommand(const std::vector<std::string> &args) { ); } else if(args.size() > 2) { - Common::Logger::log(Common::Logger::ERROR, args[0] + ": Too many arguments."); + Common::Logger::logf(Common::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); printUsage("list_hosts"); return; } @@ -168,7 +168,7 @@ void CommandParser::listHostsCommand(const std::vector<std::string> &args) { ); } else { - Common::Logger::log(Common::Logger::ERROR, args[0] + ": Don't unterstand argument '" + args[1] + "'."); + Common::Logger::logf(Common::Logger::ERROR, "%s: Don't unterstand argument '%s'.", args[0].c_str(), args[1].c_str()); printUsage("list_hosts"); return; } @@ -182,7 +182,7 @@ void CommandParser::statusCommand(const std::vector<std::string> &args) { else if(args.size() == 2) Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr<Common::RequestBase>(new Requests::DaemonStatusRequest(args[1], sigc::mem_fun(this, &CommandParser::daemonStatusRequestFinished)))); else { - Common::Logger::log(Common::Logger::ERROR, args[0] + ": Too many arguments."); + Common::Logger::logf(Common::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); printUsage("status"); return; } @@ -203,7 +203,7 @@ void CommandParser::coreStatusRequestFinished(const Common::Request<Net::Packets printHostStatus(packet); } catch(Common::Exception &exception) { - Common::Logger::log(Common::Logger::ERROR, std::string("An error occurred during your request: ") + exception.strerror() + ".\n"); + Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.\n", exception.strerror().c_str()); } requestFinished(); @@ -243,7 +243,7 @@ void CommandParser::daemonListRequestFinished(const Common::Request<Net::Packets } } catch(Common::Exception &exception) { - Common::Logger::log(Common::Logger::ERROR, std::string("An error occurred during your request: ") + exception.strerror() + ".\n"); + Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.\n", exception.strerror().c_str()); } requestFinished(); @@ -256,7 +256,7 @@ void CommandParser::daemonStatusRequestFinished(const Common::Request<Net::Packe printHostStatus(packet); } catch(Common::Exception &exception) { - Common::Logger::log(Common::Logger::ERROR, std::string("An error occurred during your request: ") + exception.strerror() + ".\n"); + Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.\n", exception.strerror().c_str()); } requestFinished(); @@ -268,7 +268,7 @@ void CommandParser::disconnectRequestFinished(const Common::Request<> &request) disconnect = true; } catch(Common::Exception &exception) { - Common::Logger::log(Common::Logger::ERROR, std::string("An error occurred during your request: ") + exception.strerror() + ".\n"); + Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.\n", exception.strerror().c_str()); } requestFinished(); @@ -344,7 +344,7 @@ bool CommandParser::parse(const std::string &cmd) { if(command) (this->*command->funcPtr)(splitCmd); else - Common::Logger::log(Common::Logger::ERROR, "Unknown command '" + splitCmd[0] + "'.\n"); + Common::Logger::logf(Common::Logger::ERROR, "Unknown command '%s'.\n", splitCmd[0].c_str()); return true; } diff --git a/src/Common/Exception.cpp b/src/Common/Exception.cpp index dc04f7d..9366fe7 100644 --- a/src/Common/Exception.cpp +++ b/src/Common/Exception.cpp @@ -43,7 +43,7 @@ std::string Exception::strerror() const { case NOT_FINISHED: return ret + "Not finished"; case INTERNAL_ERRNO: - return ret + "Internal error: " + std::strerror(subCode); + return ret + std::strerror(subCode); case INTERNAL_GNUTLS: return ret + "GnuTLS error: " + gnutls_strerror(subCode); case INVALID_ADDRESS: diff --git a/src/Common/Logger.cpp b/src/Common/Logger.cpp index e479979..22748d1 100644 --- a/src/Common/Logger.cpp +++ b/src/Common/Logger.cpp @@ -18,12 +18,42 @@ */ #include "Logger.h" +#include <cstdlib> namespace Mad { namespace Common { std::list<Logger*> Logger::loggers; + +void Logger::logfv(MessageLevel level, const char *format, va_list ap) { + int size = 100; + char *buf = (char*)std::malloc(size); + + // If buffer is too small, try again with bigger buffer + while(true) { + va_list ap2; + + va_copy(ap2, ap); + int n = std::vsnprintf(buf, size, format, ap2); + va_end(ap2); + + if(n > -1 && n < size) { + log(level, buf); + std::free(buf); + return; + } + + if(n > -1) + size = n+1; + else + size *= 2; + + buf = (char*)std::realloc(buf, size); + } + +} + void Logger::log(MessageLevel level, const std::string &message) { for(std::list<Logger*>::iterator logger = loggers.begin(); logger != loggers.end(); ++logger) { if((*logger)->getLevel() >= level) @@ -31,5 +61,21 @@ void Logger::log(MessageLevel level, const std::string &message) { } } +void Logger::logf(MessageLevel level, const char *format, ...) { + va_list ap; + + va_start(ap, format); + logfv(level, format, ap); + va_end(ap); +} + +void Logger::logf(const char *format, ...) { + va_list ap; + + va_start(ap, format); + logfv(DEFAULT, format, ap); + va_end(ap); +} + } } diff --git a/src/Common/Logger.h b/src/Common/Logger.h index 39ca5dd..f1cd33d 100644 --- a/src/Common/Logger.h +++ b/src/Common/Logger.h @@ -21,6 +21,7 @@ #define MAD_COMMON_LOGGER_H_ #include <algorithm> +#include <cstdarg> #include <list> #include <string> @@ -37,6 +38,8 @@ class Logger { static std::list<Logger*> loggers; MessageLevel level; + static void logfv(MessageLevel level, const char *format, va_list ap); + protected: Logger() : level(DEFAULT) {} @@ -48,6 +51,9 @@ class Logger { log(DEFAULT, message); } + static void logf(MessageLevel level, const char *format, ...); + static void logf(const char *format, ...); + static void registerLogger(Logger *logger) { loggers.push_back(logger); } diff --git a/src/Core/ConnectionManager.cpp b/src/Core/ConnectionManager.cpp index ceefe18..8356594 100644 --- a/src/Core/ConnectionManager.cpp +++ b/src/Core/ConnectionManager.cpp @@ -198,7 +198,7 @@ void ConnectionManager::identifyDaemonConnection(Net::Connection *connection, co daemonInfo[idCon->first].setStatus(Common::HostInfo::RUNNING); connection->setIdentified(); - Common::Logger::log("Identified as '" + name + "'."); + Common::Logger::logf("Identified as '%s'.", name.c_str()); } std::vector<Common::HostInfo> ConnectionManager::getDaemonList() const { diff --git a/src/mad.cpp b/src/mad.cpp index 62b97f1..3486a6a 100644 --- a/src/mad.cpp +++ b/src/mad.cpp @@ -76,7 +76,7 @@ int main() { Common::RequestManager::getRequestManager()->unregisterConnection(connection); } catch(Mad::Common::Exception &e) { - Common::Logger::log(Common::Logger::CRITICAL, "Connection error: " + e.strerror()); + Common::Logger::logf(Common::Logger::CRITICAL, "Connection error: %s", e.strerror().c_str()); } delete connection; diff --git a/src/madc.cpp b/src/madc.cpp index 828a2bf..b109448 100644 --- a/src/madc.cpp +++ b/src/madc.cpp @@ -121,7 +121,7 @@ int main(int argc, char *argv[]) { Common::RequestManager::getRequestManager()->unregisterConnection(connection); } catch(Mad::Common::Exception &e) { - Common::Logger::log(Common::Logger::CRITICAL, "Connection error: " + e.strerror()); + Common::Logger::logf(Common::Logger::CRITICAL, "Connection error: %s", e.strerror().c_str()); } delete connection; |