mirror of
https://github.com/eclipse/upm.git
synced 2025-03-15 04:57:30 +03:00
Merge 264f5db3ec2a61c955122b5fb47b38be2413adee into ef81a2014004bc5a6bd8df19f9edd105a122db2b
This commit is contained in:
commit
4181733454
37
examples/c++/logger.cxx
Normal file
37
examples/c++/logger.cxx
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
#include <thread>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "upm_logger.hpp"
|
||||||
|
|
||||||
|
using namespace upm;
|
||||||
|
|
||||||
|
void print()
|
||||||
|
{
|
||||||
|
UPM_LOG(LOG_DEBUG) << "Thread " << std::this_thread::get_id() << ": running loop with 3 iterations";
|
||||||
|
|
||||||
|
for (int i = 0; i < 4; ++i) {
|
||||||
|
UPM_LOG(LOG_DEBUG) << std::this_thread::get_id() << ": i = " << i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
std::vector<std::thread> threads;
|
||||||
|
|
||||||
|
UPM_LOGGER::LogLevel() = LOG_ERROR;
|
||||||
|
|
||||||
|
//UPM_LOGGER::outputFile() = "test.log";
|
||||||
|
|
||||||
|
UPM_LOG(LOG_WARNING) << "Testing the upm logger 1 " << "4";
|
||||||
|
|
||||||
|
// Launching 5 threads
|
||||||
|
for (int i = 0; i < 3; ++i) {
|
||||||
|
threads.push_back(std::thread(print));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto& thread : threads) {
|
||||||
|
thread.join();
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
8433
include/date.h
Normal file
8433
include/date.h
Normal file
File diff suppressed because it is too large
Load Diff
153
include/upm_logger.hpp
Normal file
153
include/upm_logger.hpp
Normal file
@ -0,0 +1,153 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
|
#include <mutex>
|
||||||
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
|
#include <thread>
|
||||||
|
#include "date.h"
|
||||||
|
|
||||||
|
/* Helper macro for logger utility class. */
|
||||||
|
#define UPM_LOG(log_level) \
|
||||||
|
if (log_level < UPM_LOGGER::LogLevel()) \
|
||||||
|
; \
|
||||||
|
else \
|
||||||
|
(LogHelper() && UPM_LOGGER::getInstance()).log(log_level) << __FILE__ << " " << __FUNCTION__ \
|
||||||
|
<< " " << __LINE__ << ": "
|
||||||
|
|
||||||
|
#define DEFAULT_LOG_FILE "/dev/stdout"
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
|
{
|
||||||
|
friend class LogHelper;
|
||||||
|
|
||||||
|
public:
|
||||||
|
static UPM_LOGGER&
|
||||||
|
getInstance()
|
||||||
|
{
|
||||||
|
static UPM_LOGGER instance;
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual ~UPM_LOGGER()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
std::ofstream&
|
||||||
|
log(UpmLogLevel level = LOG_ERROR)
|
||||||
|
{
|
||||||
|
using namespace date;
|
||||||
|
|
||||||
|
_logStream << "UPM - "
|
||||||
|
<< std::chrono::system_clock::now();
|
||||||
|
_logStream << " [" << getLogLevelName(level) << "] ";
|
||||||
|
|
||||||
|
return _logStream;
|
||||||
|
}
|
||||||
|
|
||||||
|
static UpmLogLevel&
|
||||||
|
LogLevel()
|
||||||
|
{
|
||||||
|
static UpmLogLevel reportingLevel = LOG_DEBUG;
|
||||||
|
|
||||||
|
return reportingLevel;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const std::string&
|
||||||
|
getLogLevelName(UpmLogLevel level)
|
||||||
|
{
|
||||||
|
if (level < LOG_ERROR || level >= NA) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return logLevelNames[level];
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::string&
|
||||||
|
outputFile()
|
||||||
|
{
|
||||||
|
static std::string file = DEFAULT_LOG_FILE;
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
UPM_LOGGER()
|
||||||
|
{
|
||||||
|
_logStream.open(outputFile(), std::ios_base::app);
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::mutex&
|
||||||
|
logMutex()
|
||||||
|
{
|
||||||
|
static std::mutex _mutex;
|
||||||
|
return _mutex;
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
UPM_LOGGER(const UPM_LOGGER&) = delete;
|
||||||
|
UPM_LOGGER& operator=(const UPM_LOGGER&) = delete;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::ofstream _logStream;
|
||||||
|
};
|
||||||
|
|
||||||
|
/* This class exists only to append a newline when the log statement ends.*/
|
||||||
|
class LogHelper
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
LogHelper()
|
||||||
|
{
|
||||||
|
_mutex = &UPM_LOGGER::logMutex();
|
||||||
|
_mutex->lock();
|
||||||
|
_os = &UPM_LOGGER::getInstance()._logStream;
|
||||||
|
}
|
||||||
|
|
||||||
|
~LogHelper()
|
||||||
|
{
|
||||||
|
*(_os) << std::endl;
|
||||||
|
_mutex->unlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
friend UPM_LOGGER&
|
||||||
|
operator&&(const LogHelper& l, UPM_LOGGER& r)
|
||||||
|
{
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::ofstream* _os;
|
||||||
|
std::mutex* _mutex;
|
||||||
|
};
|
||||||
|
}
|
@ -18,6 +18,12 @@ target_link_libraries(json_tests GTest::GTest GTest::Main)
|
|||||||
target_include_directories(json_tests PRIVATE "${UPM_COMMON_HEADER_DIRS}/")
|
target_include_directories(json_tests PRIVATE "${UPM_COMMON_HEADER_DIRS}/")
|
||||||
gtest_add_tests(json_tests "" AUTO)
|
gtest_add_tests(json_tests "" AUTO)
|
||||||
|
|
||||||
|
# Unit tests - logger header library
|
||||||
|
add_executable(logger_tests logger/logger_tests.cxx)
|
||||||
|
target_link_libraries(logger_tests pthread GTest::GTest GTest::Main)
|
||||||
|
target_include_directories(logger_tests PRIVATE "${UPM_COMMON_HEADER_DIRS}/")
|
||||||
|
gtest_add_tests(logger_tests "" AUTO)
|
||||||
|
|
||||||
# Unit tests - nmea_gps library
|
# Unit tests - nmea_gps library
|
||||||
add_executable(nmea_gps_tests nmea_gps/nmea_gps_tests.cxx)
|
add_executable(nmea_gps_tests nmea_gps/nmea_gps_tests.cxx)
|
||||||
target_link_libraries(nmea_gps_tests nmea_gps GTest::GTest GTest::Main)
|
target_link_libraries(nmea_gps_tests nmea_gps GTest::GTest GTest::Main)
|
||||||
@ -28,5 +34,6 @@ add_custom_target(tests-unit ALL
|
|||||||
DEPENDS
|
DEPENDS
|
||||||
utilities_tests
|
utilities_tests
|
||||||
json_tests
|
json_tests
|
||||||
|
logger_tests
|
||||||
COMMENT "UPM unit test collection")
|
COMMENT "UPM unit test collection")
|
||||||
|
|
||||||
|
135
tests/unit/logger/logger_tests.cxx
Normal file
135
tests/unit/logger/logger_tests.cxx
Normal file
@ -0,0 +1,135 @@
|
|||||||
|
#include "upm_logger.hpp"
|
||||||
|
#include "gtest/gtest.h"
|
||||||
|
#include <cstdio>
|
||||||
|
#include <fstream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#define LOG_FILE "test_log_file"
|
||||||
|
|
||||||
|
size_t logCount = 0;
|
||||||
|
|
||||||
|
using namespace upm;
|
||||||
|
|
||||||
|
/* Utilities test fixture */
|
||||||
|
class logger_unit : public ::testing::Test
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
/* One-time setup logic if needed */
|
||||||
|
logger_unit()
|
||||||
|
{
|
||||||
|
UPM_LOGGER::outputFile() = LOG_FILE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* One-time tear-down logic if needed */
|
||||||
|
virtual ~logger_unit()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Per-test setup logic if needed */
|
||||||
|
virtual void
|
||||||
|
SetUp()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Per-test tear-down logic if needed */
|
||||||
|
virtual void
|
||||||
|
TearDown()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
logFileExists(const std::string& logFile)
|
||||||
|
{
|
||||||
|
std::ifstream f(logFile.c_str());
|
||||||
|
return f.good();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
deleteLogFile(const std::string& logFile)
|
||||||
|
{
|
||||||
|
if (logFileExists(logFile)) {
|
||||||
|
if (remove(logFile.c_str()) != 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::ifstream&
|
||||||
|
goToLine(std::ifstream& file, unsigned line)
|
||||||
|
{
|
||||||
|
file.seekg(std::ios::beg);
|
||||||
|
for (unsigned i = 0; i < line - 1; ++i) {
|
||||||
|
file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::size_t
|
||||||
|
getMsgPosition(std::string& line)
|
||||||
|
{
|
||||||
|
std::size_t pos = line.find_first_of(':');
|
||||||
|
|
||||||
|
for (int i = 0; i < 2 && pos != std::string::npos; ++i) {
|
||||||
|
pos = line.find_first_of(':', pos + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (pos + 2);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/* Test log file creation. */
|
||||||
|
TEST_F(logger_unit, test_logger_create_log_file)
|
||||||
|
{
|
||||||
|
// We actually have to write a log because of the lazy instantiation.
|
||||||
|
UPM_LOG(LOG_DEBUG);
|
||||||
|
logCount++;
|
||||||
|
|
||||||
|
ASSERT_TRUE(logFileExists(LOG_FILE));
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Test simple log. */
|
||||||
|
TEST_F(logger_unit, test_logger_simple_log)
|
||||||
|
{
|
||||||
|
const std::string log = "log sample for gtest";
|
||||||
|
std::ifstream infile(LOG_FILE);
|
||||||
|
std::string line;
|
||||||
|
|
||||||
|
UPM_LOG(LOG_DEBUG) << log;
|
||||||
|
logCount++;
|
||||||
|
|
||||||
|
goToLine(infile, logCount);
|
||||||
|
std::getline(infile, line);
|
||||||
|
ASSERT_STREQ(log.c_str(), line.substr(getMsgPosition(line)).c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Test multiple logs. */
|
||||||
|
TEST_F(logger_unit, test_logger_multiple_logs)
|
||||||
|
{
|
||||||
|
const std::string logs[] = { "log1", "log2", "log3" };
|
||||||
|
|
||||||
|
std::ifstream infile(LOG_FILE);
|
||||||
|
std::string line;
|
||||||
|
|
||||||
|
for (int i = 0; i < 3; ++i) {
|
||||||
|
UPM_LOG(LOG_DEBUG) << logs[i];
|
||||||
|
logCount++;
|
||||||
|
goToLine(infile, logCount);
|
||||||
|
std::getline(infile, line);
|
||||||
|
ASSERT_STREQ(logs[i].c_str(), line.substr(getMsgPosition(line)).c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Test log levels. */
|
||||||
|
TEST_F(logger_unit, test_logger_log_level)
|
||||||
|
{
|
||||||
|
// By default, UPM_LOGGER logs with DEBUG level.
|
||||||
|
UPM_LOGGER::LogLevel() = LOG_ERROR;
|
||||||
|
ASSERT_STREQ(UPM_LOGGER::getLogLevelName(UPM_LOGGER::LogLevel()).c_str(), "ERROR");
|
||||||
|
UPM_LOGGER::LogLevel() = LOG_DEBUG;
|
||||||
|
ASSERT_STREQ(UPM_LOGGER::getLogLevelName(UPM_LOGGER::LogLevel()).c_str(), "DEBUG");
|
||||||
|
|
||||||
|
deleteLogFile(LOG_FILE);
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user