UPnPsdk 0.1
Universal Plug and Play +, Software Development Kit
 
Loading...
Searching...
No Matches
synclog.hpp
Go to the documentation of this file.
1#ifndef UPnPsdk_SYNCLOG_HPP
2#define UPnPsdk_SYNCLOG_HPP
3// Copyright (C) 2024+ GPL 3 and higher by Ingo Höft, <Ingo@Hoeft-online.de>
4// Redistribution only with this Copyright remark. Last modified: 2025-05-04
11#include <cmake_vars.hpp>
12#include <UPnPsdk/global.hpp>
13#include <UPnPsdk/pthread.hpp>
15#include <string>
16#include <iostream>
17#ifndef __APPLE__
18#include <syncstream>
19#endif
20
21namespace UPnPsdk {
22
23// clang-format off
24
25// Usage: SYNC(std::cout) << "Message\n";
26// Usage: SYNC(std::cerr) << "Error\n";
27#ifdef __APPLE__
28 #define SYNC(s) (s)
29#else
30 #define SYNC(s) std::osyncstream((s))
31#endif
32
33
34// Trace messages
35// --------------
36#ifdef UPnPsdk_WITH_TRACE
37 #define TRACE(s) SYNC(std::cerr)<<"TRACE["<<(static_cast<const char*>(__FILE__) + CMAKE_SOURCE_PATH_LENGTH)<<":"<<__LINE__<<"] "<<(s)<<"\n";
38 #define TRACE2(a, b) SYNC(std::cerr)<<"TRACE["<<(static_cast<const char*>(__FILE__) + CMAKE_SOURCE_PATH_LENGTH)<<":"<<__LINE__<<"] "<<(a)<<(b)<<"\n";
39#else // no UPnPsdk_WITH_TRACE
40 #define TRACE(s)
41 #define TRACE2(a, b)
42#endif
43
44
45// Debug output messages with some that can be enabled during runtime.
46// -------------------------------------------------------------------
47// __func__ is defined for POSIX so we have it there for the
48// function signature to output for information. On MSC_VER it is named
49// __FUNCTION__. __PRETTY_FUNCTION__ is also available.
50#if defined(_MSC_VER) || defined(__APPLE__)
51#define __func__ __FUNCTION__
52#define PTHREAD_SELF UPnPsdk::pthread_self()
53// or more verbose: #define __PRETTY_FUNCTION__ __FUNCSIG__
54#else
55#define PTHREAD_SELF ::pthread_self()
56#endif
57
58// This is intended to be used as:
59// throw(UPnPsdk_LOGEXCEPT("MSG1nnn") "exception message.\n");
60#define UPnPsdk_LOGEXCEPT(m) "UPnPsdk "+std::string(m)+" EXCEPT[#"+std::to_string(PTHREAD_SELF)+" "+::std::string(__func__)+"()] "
61#define UPnPsdk_LOGWHAT "UPnPsdk ["+::std::string(__func__)+"] WHAT "
62
63// Next line mainly used for Unit Tests to catch the right output channel.
64inline constexpr int log_fileno{2}; // 1 = stdout, 2 = stderr, conforming to next line
65#define UPnPsdk_LOG(m) SYNC(std::cerr)<<"UPnPsdk "<<(m)
66
67// Critical messages are always output.
68#define UPnPsdk_LOGCRIT(m) UPnPsdk_LOG(m)<<" CRIT [#"<<PTHREAD_SELF<<" "<<__func__<<"()] "
69// Messages only with debug enabled.
70#define UPnPsdk_LOGERR(m) if(UPnPsdk::g_dbug) UPnPsdk_LOG(m)<<" ERROR [#"<<PTHREAD_SELF<<" "<<__func__<<"()] "
71#define UPnPsdk_LOGCATCH(m) if(UPnPsdk::g_dbug) UPnPsdk_LOG(m)<<" CATCH [#"<<PTHREAD_SELF<<" "<<__func__<<"()] "
72#define UPnPsdk_LOGINFO(m) if(UPnPsdk::g_dbug) UPnPsdk_LOG(m)<<" INFO [#"<<PTHREAD_SELF<<" "<<__func__<<"()] "
73
74// clang-format on
75} // namespace UPnPsdk
77
78#endif // UPnPsdk_SYNCLOG_HPP
Global used constants and variables.
Reengineered Object Oriented UPnP+ program code.
Additional functions to manage Posix threads with C++ to be portable.