From 6aa9bd0f77dcb5128167fae62e32aa5252fe85c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Mon, 2 Dec 2013 00:55:24 +0100 Subject: Renew the updater branch Now with some actual consensus on what the updater will do! --- mmc_updater/src/Log.cpp | 65 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 mmc_updater/src/Log.cpp (limited to 'mmc_updater/src/Log.cpp') diff --git a/mmc_updater/src/Log.cpp b/mmc_updater/src/Log.cpp new file mode 100644 index 00000000..d4e5a214 --- /dev/null +++ b/mmc_updater/src/Log.cpp @@ -0,0 +1,65 @@ +#include "Log.h" + +#include "Platform.h" +#include "StringUtils.h" +#include "ProcessUtils.h" + +#include +#include + +Log m_globalLog; + +Log* Log::instance() +{ + return &m_globalLog; +} + +Log::Log() +{ +} + +Log::~Log() +{ +} + +void Log::open(const std::string& path) +{ + m_mutex.lock(); + m_output.open(path.c_str(),std::ios_base::out | std::ios_base::app); + m_mutex.unlock(); +} + +void Log::writeToStream(std::ostream& stream, Type type, const char* text) +{ + // Multiple processes may be writing to the same log file during + // an update. No attempt is made to synchronize access to the file. + // + // Under Unix, appends to a single file on a local FS by multiple writers should be atomic + // provided that the length of 'text' is less than PIPE_BUF + // + switch (type) + { + case Info: + stream << "INFO "; + break; + case Warn: + stream << "WARN "; + break; + case Error: + stream << "ERROR "; + break; + } + stream << '(' << intToStr(ProcessUtils::currentProcessId()) << ") " << text << std::endl; +} + +void Log::write(Type type, const char* text) +{ + m_mutex.lock(); + writeToStream(std::cerr,type,text); + if (m_output.is_open()) + { + writeToStream(m_output,type,text); + } + m_mutex.unlock(); +} + -- cgit v1.2.3