map/das-dn/third_party/AdsLib/Log.cpp

56 lines
1.5 KiB
C++
Raw Normal View History

2024-12-03 10:36:06 +08:00
// SPDX-License-Identifier: MIT
/**
Copyright (c) 2015 -2022 Beckhoff Automation GmbH & Co. KG
Author: Patrick Bruenn <p.bruenn@beckhoff.com>
*/
#include "Log.h"
2024-12-05 11:09:23 +08:00
#if 0
2024-12-03 10:36:06 +08:00
#include <algorithm>
#include <chrono>
#include <ctime>
#include <fstream>
#include <iostream>
2024-12-05 11:09:23 +08:00
#endif
2024-12-03 10:36:06 +08:00
#include "public.h"
2024-12-05 11:09:23 +08:00
#if 0
2024-12-03 10:36:06 +08:00
#ifdef _WIN32
#define TIME_T_TO_STRING(DATE_TIME, TIME_T) do { \
struct tm temp; \
localtime_s(&temp, TIME_T); \
2024-12-05 11:04:47 +08:00
std::strftime(DATE_TIME, sizeof(DATE_TIME), "%Y-%m-%dT%H:%M:%S ", &temp); \
2024-12-03 10:36:06 +08:00
} while (0);
#elif defined(__CYGWIN__)
#define TIME_T_TO_STRING(DATE_TIME, TIME_T) std::strftime(DATE_TIME, sizeof(DATE_TIME), "%FT%T ", localtime(TIME_T));
#else
2024-12-05 11:04:47 +08:00
#define TIME_T_TO_STRING(DATE_TIME, TIME_T) std::strftime(DATE_TIME, sizeof(DATE_TIME), "%FT%T%z ", localtime(TIME_T));
2024-12-03 10:36:06 +08:00
#endif
2024-12-05 11:09:23 +08:00
#endif
2024-12-03 10:36:06 +08:00
size_t Logger::logLevel = CONFIG_DEFAULT_LOGLEVEL;
2024-12-05 11:04:47 +08:00
#if 0
2024-12-03 10:36:06 +08:00
static const char* CATEGORY[] = {
2024-12-05 11:04:47 +08:00
"Verbose: ",
"Info: ",
"Warning: ",
"Error: "
2024-12-03 10:36:06 +08:00
};
2024-12-05 11:04:47 +08:00
#endif
2024-12-03 10:36:06 +08:00
void Logger::Log(const size_t level, const std::string& msg)
{
2024-12-09 09:44:14 +08:00
if (level >= logLevel)
{
2024-12-03 10:36:06 +08:00
#if 0
2024-12-05 11:04:47 +08:00
std::time_t tt = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
2024-12-03 10:36:06 +08:00
const auto category = CATEGORY[std::min(level, sizeof(CATEGORY) / sizeof(CATEGORY[0]))];
char dateTime[28];
//TODO use std::put_time() when available
TIME_T_TO_STRING(dateTime, &tt);
2024-12-05 11:04:47 +08:00
std::cerr << dateTime << category << msg << std::endl;
2024-12-03 10:36:06 +08:00
#endif
vLog((eLogLevel)level, "%s\n", msg.c_str());
}
}