ivi-main-loop
ivi-main-loop-log.h
1  /* Copyright (C) 2015 Jacques Guillou */
2  /* Copyright (C) 2015 Pelagicore AB <info@pelagicore.com> */
3 
4  /* This library is free software; you can redistribute it and/or */
5  /* modify it under the terms of the GNU Library General Public */
6  /* License as published by the Free Software Foundation; either */
7  /* version 2 of the License, or (at your option) any later version. */
8 
9  /* This library is distributed in the hope that it will be useful, */
10  /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
11  /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
12  /* Library General Public License for more details. */
13 
14  /* A copy of the GNU Library General Public License is included in the */
15  /* "LICENSE" file. */
16 
17 #pragma once
18 
19 #include <iostream>
20 
21 #ifdef USE_IVI_LOGGING
22 #include "ivi-logging.h"
23 #else
24 #define log_debug() ivi::Log(std::cout).stream()
25 #define log_error() ivi::Log(std::cerr).stream()
26 #define LOG_DECLARE_CONTEXT(args ...)
27 #endif
28 
29 namespace ivi {
30 
31 #ifdef USE_IVI_LOGGING
32 typedef logging::DefaultLogContext LogContext;
33 LOG_IMPORT_DEFAULT_CONTEXT(iviMainLoopContext);
34 #else
35 
36 // Convenience class which automatically writes std::endl to the stream during destruction, for compatibility with ivi-logging
37 class Log
38 {
39 public:
40  Log(std::ostream &s)
41  {
42  m_stream = &s;
43  }
44 
45  ~Log()
46  {
47  *m_stream << std::endl;
48  }
49 
50  std::ostream &stream()
51  {
52  return *m_stream;
53  }
54 
55 private:
56  std::ostream *m_stream = nullptr;
57 };
58 #endif
59 
60 }