ifm3d
log_writer_console.h
1
// -*- c++ -*-
2
/*
3
* Copyright 2023-present ifm electronic, gmbh
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
7
#ifndef IFM3D_COMMON_LOGGING_LOG_WRITER_CONSOLE_H
8
#define IFM3D_COMMON_LOGGING_LOG_WRITER_CONSOLE_H
9
10
#include <cstdint>
11
#include <cstdio>
12
#include <cstring>
13
#include <ifm3d/common/logging/log_level.h>
14
#include <iostream>
15
#include <mutex>
16
17
#if defined(_MSC_VER)
18
# include <io.h>
19
# define IS_A_TTY(stream) (!!_isatty(_fileno(stream)))
20
#else
21
# include <unistd.h>
22
# define IS_A_TTY(stream) (!!isatty(fileno(stream)))
23
#endif
24
25
#include <ifm3d/common/logging/log_writer.h>
26
27
namespace
ifm3d
28
{
29
enum class
Output : std::uint8_t
30
{
31
StdOut,
32
StdErr,
33
};
34
35
template
<
class
FORMATTER,
36
typename
std::enable_if_t<std::is_same_v<
37
decltype(FORMATTER::Format(
38
ifm3d::LogEntry
(
""
, ifm3d::LogLevel::Info,
""
,
""
, 1))),
39
std::string>>* =
nullptr
>
40
class
LogWriterConsole
:
public
LogWriter
41
{
42
public
:
43
LogWriterConsole
(Output out = Output::StdErr)
44
: _out(out == Output::StdOut ? std::cout : std::cerr),
45
_is_a_tty(IS_A_TTY(out == Output::StdOut ? stdout : stderr))
46
{}
47
48
void
49
Write(
const
LogEntry
& entry)
override
50
{
51
const
auto
str = FORMATTER::Format(entry);
52
const
std::lock_guard<std::mutex> lock(this->_mutex);
53
this->_out << str << std::endl;
54
}
55
56
protected
:
57
std::mutex _mutex;
58
std::ostream& _out;
// NOLINT(*-avoid-const-or-ref-data-members)
59
bool
_is_a_tty;
60
};
61
}
62
#endif // IFM3D_COMMON_LOGGING_LOG_WRITER_CONSOLE_H
ifm3d::LogWriter
Definition:
log_writer.h:14
ifm3d::LogEntry
Definition:
log_entry.h:19
ifm3d::LogWriterConsole
Definition:
log_writer_console.h:40
ifm3d
common
logging
log_writer_console.h
Generated by
1.8.17