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 <mutex>
11
#include <cstring>
12
#include <cstdio>
13
#include <iostream>
14
15
#if defined(_MSC_VER)
16
# include <io.h>
17
# define IS_A_TTY(stream) (!!_isatty(_fileno(stream)))
18
#else
19
# include <unistd.h>
20
# define IS_A_TTY(stream) (!!isatty(fileno(stream)))
21
#endif
22
23
#include <ifm3d/common/logging/log_writer.h>
24
25
namespace
ifm3d
26
{
27
enum class
Output
28
{
29
StdOut,
30
StdErr,
31
};
32
33
template
<
class
Formatter,
34
typename
std::enable_if_t<std::is_same_v<
35
decltype(Formatter::format(
36
ifm3d::LogEntry
(
""
, ifm3d::LogLevel::Info,
""
,
""
, 1))),
37
std::string>>* =
nullptr
>
38
class
LogWriterConsole
:
public
LogWriter
39
{
40
public
:
41
LogWriterConsole
(Output out = Output::StdErr)
42
: out_(out == Output::StdOut ? std::cout : std::cerr),
43
is_a_tty_(IS_A_TTY(out == Output::StdOut ? stdout : stderr))
44
{}
45
46
void
47
Write(
const
LogEntry
& entry)
override
48
{
49
const
auto
str = Formatter::format(entry);
50
const
std::lock_guard<std::mutex> lock(this->mutex_);
51
this->out_ << str << std::endl;
52
}
53
54
protected
:
55
std::mutex mutex_;
56
std::ostream& out_;
57
bool
is_a_tty_;
58
};
59
}
60
#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:38
ifm3d
common
logging
log_writer_console.h
Generated by
1.8.17