mirror of
https://github.com/eclipse/upm.git
synced 2025-03-15 04:57:30 +03:00
example for logger class + minor improvements
This commit is contained in:
parent
046ac70071
commit
43ecacfce7
@ -1,16 +1,34 @@
|
|||||||
|
#include <thread>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include "upm_logger.hpp"
|
#include "upm_logger.hpp"
|
||||||
|
|
||||||
using namespace upm;
|
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()
|
int main()
|
||||||
{
|
{
|
||||||
|
std::vector<std::thread> threads;
|
||||||
|
|
||||||
UPM_LOGGER::LogLevel() = LOG_INFO;
|
UPM_LOGGER::LogLevel() = LOG_INFO;
|
||||||
|
|
||||||
UPM_LOG(LOG_WARNING) << "a loop with 4 iterations";
|
UPM_LOG(LOG_WARNING) << "Testing the upm logger";
|
||||||
|
|
||||||
for (int i = 0; i < 4; ++i) {
|
// Launching 5 threads
|
||||||
UPM_LOG(LOG_DEBUG) << "i = " << i;
|
for (int i = 0; i < 5; ++i) {
|
||||||
|
threads.push_back(std::thread(print));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto& thread : threads) {
|
||||||
|
thread.join();
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -1,3 +1,27 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
#pragma once
|
||||||
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
@ -38,13 +62,10 @@ namespace upm {
|
|||||||
|
|
||||||
class UPM_LOGGER {
|
class UPM_LOGGER {
|
||||||
public:
|
public:
|
||||||
UPM_LOGGER();
|
UPM_LOGGER() {}
|
||||||
virtual ~UPM_LOGGER();
|
virtual ~UPM_LOGGER();
|
||||||
std::ostringstream& log(UpmLogLevel level = LOG_ERROR);
|
std::ostringstream& log(UpmLogLevel level = LOG_ERROR);
|
||||||
static UpmLogLevel& LogLevel();
|
static UpmLogLevel& LogLevel();
|
||||||
|
|
||||||
static std::ofstream logStream;
|
|
||||||
static std::mutex logMutex;
|
|
||||||
protected:
|
protected:
|
||||||
std::ostringstream os;
|
std::ostringstream os;
|
||||||
private:
|
private:
|
||||||
@ -53,32 +74,33 @@ namespace upm {
|
|||||||
private:
|
private:
|
||||||
// Track logLevel for this particular instance of UPM_LOGGER
|
// Track logLevel for this particular instance of UPM_LOGGER
|
||||||
UpmLogLevel logLevel;
|
UpmLogLevel logLevel;
|
||||||
|
std::ofstream logStream;
|
||||||
|
static std::mutex logMutex;
|
||||||
public:
|
public:
|
||||||
static const std::string& getLogLevelName(UpmLogLevel level)
|
static const std::string& getLogLevelName(UpmLogLevel level);
|
||||||
{
|
private:
|
||||||
if (level < LOG_ERROR || level >= NA) {
|
void write(std::ostringstream& os);
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return logLevelNames[level];
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
|
||||||
static void write(std::ostringstream& os)
|
|
||||||
{
|
|
||||||
|
|
||||||
|
|
||||||
std::lock_guard<std::mutex> lock(logMutex);
|
|
||||||
|
|
||||||
logStream.open(LOG_FILE, std::ios_base::app);
|
|
||||||
logStream << os.str();
|
|
||||||
logStream.flush();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
UPM_LOGGER::UPM_LOGGER() {}
|
std::mutex UPM_LOGGER::logMutex;
|
||||||
|
|
||||||
|
void UPM_LOGGER::write(std::ostringstream& os)
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(logMutex);
|
||||||
|
|
||||||
|
logStream.open(LOG_FILE, std::ios_base::app);
|
||||||
|
logStream << os.str();
|
||||||
|
logStream.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string& UPM_LOGGER::getLogLevelName(UpmLogLevel level)
|
||||||
|
{
|
||||||
|
if (level < LOG_ERROR || level >= NA) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return logLevelNames[level];
|
||||||
|
}
|
||||||
|
|
||||||
UpmLogLevel& UPM_LOGGER::LogLevel()
|
UpmLogLevel& UPM_LOGGER::LogLevel()
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user