Files
openvehicle-api/global/trace.h
tompzf 6ed4b1534e Precommit (#1)
* first commit

* cleanup
2025-11-04 13:28:06 +01:00

77 lines
1.9 KiB
C++

#ifndef TRACE_H
#define TRACE_H
#include <chrono>
#include <ctime>
#include <sstream>
#include <iomanip>
#ifdef __GNUC__
#include <unistd.h>
#endif
#ifndef ENABLE_TRACE
/**
* @brief Enable the trace macro by setting to a non-zero value.
*/
#define ENABLE_TRACE 0
#endif
/**
* @brief Return the current time as a string.
* @return The current time.
*/
inline std::string GetTimestamp()
{
const auto current_time_point {std::chrono::system_clock::now()};
const auto current_time {std::chrono::system_clock::to_time_t(current_time_point)};
const auto current_localtime {*std::localtime (&current_time)};
const auto current_time_since_epoch {current_time_point.time_since_epoch()};
const auto current_milliseconds {std::chrono::duration_cast<std::chrono::milliseconds> (current_time_since_epoch).count() % 1000};
std::ostringstream stream;
stream << "PID#" << std::dec << getpid() << " " << std::put_time(&current_localtime, "%H:%M:%S") << "." << std::setw(3) << std::setfill('0') << current_milliseconds << ": ";
return stream.str();
}
#include <cstring>
#include <iostream>
#if ENABLE_TRACE != 0
#ifdef _MSC_VER
#define TRACE(...) Trace(GetTimestamp(), __FUNCTION__, ": ", __VA_ARGS__)
#elif defined __GNUC__
#define TRACE(...) Trace(GetTimestamp(), __PRETTY_FUNCTION__, ": ", ##__VA_ARGS__)
#else
#error Other compiler are not supported.
#endif
inline void Trace(std::stringstream& /*rsstream*/)
{}
template <typename TArg, typename... TArgs>
inline void Trace(std::stringstream& rsstream, TArg tArg, TArgs... tArgs)
{
rsstream << tArg;
Trace(rsstream, tArgs...);
}
template <typename... TArgs>
inline void Trace(TArgs... tArgs)
{
std::stringstream sstream;
Trace(sstream, tArgs...);
sstream << std::endl;
std::cout << sstream.str();
}
#else // ENABLE_TRACE == 0
inline void Trace()
{}
#ifdef __GNUC__
#endif
#define TRACE(...) Trace()
#endif
#endif // !defined TRACE_H