upm/include/upm_logger.hpp

132 lines
3.3 KiB
C++
Raw Normal View History

/*
* Author: Mihai Stefanescu <mihai.stefanescu@rinftech.com>
* Copyright (c) 2018 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
2018-05-21 19:13:18 +03:00
#pragma once
#include <sstream>
2018-06-05 11:59:04 +03:00
#include <fstream>
2018-05-21 19:13:18 +03:00
#include <string>
#include <chrono>
2018-06-05 11:59:04 +03:00
#include <thread>
#include <mutex>
2018-05-21 19:13:18 +03:00
2018-05-22 17:33:49 +03:00
/* Helper macro for logger utility class. */
#define UPM_LOG(log_level) \
if ( log_level < UPM_LOGGER::LogLevel() ) \
; \
else \
UPM_LOGGER().log(log_level) << __FILE__ << " " \
<< __FUNCTION__ << " " \
<< __LINE__ << ": "
2018-05-22 17:33:49 +03:00
2018-06-05 11:59:04 +03:00
#define LOG_FILE "/var/log/upm.log"
2018-05-21 19:13:18 +03:00
namespace upm {
enum UpmLogLevel {
LOG_ERROR,
LOG_WARNING,
LOG_INFO,
LOG_DEBUG,
NA
};
static const std::string logLevelNames[] = {
"ERROR",
"WARNING",
"INFO",
"DEBUG",
""
};
class UPM_LOGGER {
public:
UPM_LOGGER() {}
2018-05-21 19:13:18 +03:00
virtual ~UPM_LOGGER();
2018-05-22 17:33:49 +03:00
std::ostringstream& log(UpmLogLevel level = LOG_ERROR);
static UpmLogLevel& LogLevel();
2018-05-21 19:13:18 +03:00
protected:
std::ostringstream os;
private:
UPM_LOGGER(const UPM_LOGGER&);
UPM_LOGGER& operator=(const UPM_LOGGER&);
private:
2018-05-22 17:33:49 +03:00
// Track logLevel for this particular instance of UPM_LOGGER
2018-05-21 19:13:18 +03:00
UpmLogLevel logLevel;
std::ofstream logStream;
static std::mutex logMutex;
2018-05-21 19:13:18 +03:00
public:
static const std::string& getLogLevelName(UpmLogLevel level);
private:
void write(std::ostringstream& os);
};
2018-06-05 11:59:04 +03:00
std::mutex UPM_LOGGER::logMutex;
2018-06-05 11:59:04 +03:00
void UPM_LOGGER::write(std::ostringstream& os)
{
std::lock_guard<std::mutex> lock(logMutex);
2018-06-05 11:59:04 +03:00
logStream.open(LOG_FILE, std::ios_base::app);
logStream << os.str();
logStream.flush();
}
2018-06-05 11:59:04 +03:00
const std::string& UPM_LOGGER::getLogLevelName(UpmLogLevel level)
{
if (level < LOG_ERROR || level >= NA) {
return NULL;
2018-06-05 11:59:04 +03:00
}
2018-05-21 19:13:18 +03:00
return logLevelNames[level];
}
2018-05-21 19:13:18 +03:00
2018-05-22 17:33:49 +03:00
UpmLogLevel& UPM_LOGGER::LogLevel()
2018-05-21 19:13:18 +03:00
{
static UpmLogLevel reportingLevel = LOG_DEBUG;
2018-05-22 17:33:49 +03:00
2018-05-21 19:13:18 +03:00
return reportingLevel;
}
2018-05-22 17:33:49 +03:00
std::ostringstream& UPM_LOGGER::log(UpmLogLevel level)
2018-05-21 19:13:18 +03:00
{
using namespace std::chrono;
os << "UPM - " << duration_cast<nanoseconds>
2018-05-21 19:13:18 +03:00
(system_clock::now().time_since_epoch()).count();
os << " " << getLogLevelName(level) << " ";
2018-05-22 17:33:49 +03:00
2018-05-21 19:13:18 +03:00
logLevel = level;
return os;
}
UPM_LOGGER::~UPM_LOGGER()
{
2018-05-22 17:33:49 +03:00
os << std::endl;
2018-06-05 11:59:04 +03:00
write(os);
2018-05-21 19:13:18 +03:00
}
}