Keyple Util C++ Library - 2.4.0
Component of the Keyple C++ middleware
Logger.cpp
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (c) 2025 Calypso Networks Association https://calypsonet.org/ *
3 * *
4 * See the NOTICE file(s) distributed with this work for additional *
5 * information regarding copyright ownership. *
6 * *
7 * This program and the accompanying materials are made available under the *
8 * terms of the Eclipse Public License 2.0 which is available at *
9 * http://www.eclipse.org/legal/epl-2.0 *
10 * *
11 * SPDX-License-Identifier: EPL-2.0 *
12 ******************************************************************************/
13
14#include "keyple/core/util/cpp/Logger.hpp"
15
16#include <chrono>
17#include <cstdarg>
18#include <cstdio>
19#include <ctime>
20#include <string>
21
22namespace keyple {
23namespace core {
24namespace util {
25namespace cpp {
26
27Logger::Level Logger::mLevel = Logger::Level::logDebug;
28
29Logger::Logger(const std::string& className, std::mutex* mtx)
30: className(demangle(className.c_str()))
31, mtx(mtx)
32{
33}
34
35std::string
36Logger::getClassName()
37{
38 return className;
39}
40
41void
42Logger::setLoggerLevel(Logger::Level level)
43{
44 mLevel = level;
45}
46
47const std::string
48Logger::getCurrentTimestamp()
49{
50 using std::chrono::system_clock;
51 auto currentTime = std::chrono::system_clock::now();
52 char buffer1[21];
53 char buffer2[26];
54
55 auto transformed = currentTime.time_since_epoch().count() / 1000000;
56 auto millis = transformed % 1000;
57
58 tm ttm;
59 std::time_t tt = system_clock::to_time_t(currentTime);
60#if defined(WIN32) || defined(_WIN32) \
61 || defined(__WIN32) && !defined(__CYGWIN__)
62 localtime_s(&ttm, &tt);
63#else
64 localtime_r(&tt, &ttm);
65#endif
66
67 strftime(buffer1, sizeof(buffer1), "%F %H:%M:%S", &ttm);
68 snprintf(
69 buffer2, sizeof(buffer2), "%s:%03d", buffer1, static_cast<int>(millis));
70
71 return std::string(buffer2);
72}
73
74} /* namespace cpp */
75} /* namespace util */
76} /* namespace core */
77} /* namespace keyple */