casadi_logger.hpp
1 /*
2  * This file is part of CasADi.
3  *
4  * CasADi -- A symbolic framework for dynamic optimization.
5  * Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl,
6  * KU Leuven. All rights reserved.
7  * Copyright (C) 2011-2014 Greg Horn
8  *
9  * CasADi is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 3 of the License, or (at your option) any later version.
13  *
14  * CasADi is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with CasADi; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 
26 #ifndef CASADI_LOGGER_HPP
27 #define CASADI_LOGGER_HPP
28 
29 #include <casadi/core/casadi_export.h>
30 
31 #include <cstdarg>
32 #include <fstream>
33 #include <iostream>
34 
35 namespace casadi {
44  class CASADI_EXPORT Logger {
45  private:
47  Logger();
48 
49  public:
51  static void (*writeFun)(const char* s, std::streamsize num, bool error);
52 
54  static void (*flush)(bool error);
55 
56  static void WriteFunThreadSafe(const char* s, std::streamsize num, bool error);
57  static void FlushThreadSafe(bool error);
58 
60  static void writeDefault(const char* s, std::streamsize num, bool error) {
61  if (error) {
62  std::cerr.write(s, num);
63  } else {
64  std::cout.write(s, num);
65  }
66  }
67 
69  static void flushDefault(bool error) {
70  if (error) {
71  std::cerr << std::flush;
72  } else {
73  std::cout << std::flush;
74  }
75  }
76 
78  static void writeIgnore(const char* s, std::streamsize num, bool error) {
79  }
80 
82  template<bool Err> static void write(const char* s, std::streamsize num) {
83  // All information
84  WriteFunThreadSafe(s, num, Err);
85  }
86 
88  template<bool Err> static void writeCh(char ch) {
89  write<Err>(&ch, 1);
90  }
91 
92  // Stream buffer for std::cout like printing
93  template<bool Err> class Streambuf : public std::streambuf {
94  public:
95  Streambuf() {}
96  protected:
97  int_type overflow(int_type ch) override {
98  if (ch != traits_type::eof()) {
99  writeCh<Err>(static_cast<char>(ch));
100  }
101  return ch;
102  }
103  std::streamsize xsputn(const char* s, std::streamsize num) override {
104  write<Err>(s, num);
105  return num;
106  }
107  int sync() override {
108  FlushThreadSafe(Err);
109  return 0;
110  }
111  };
112 
113  // Output stream for std::cout like printing
114  template<bool Err> class Stream : public std::ostream {
115  protected:
117  public:
118  Stream() : std::ostream(&buf) {}
119  };
120  };
121 
122  // Get an output stream
123  CASADI_EXPORT std::ostream& uout();
124 
125  // Get an output stream
126  CASADI_EXPORT std::ostream& uerr();
127 
128 } // namespace casadi
129 
130 extern "C" {
131  CASADI_EXPORT int casadi_printf(const char *fmt, ...);
132 }
133 
134 #endif // CASADI_LOGGER_HPP
Streambuf< Err > buf
std::streamsize xsputn(const char *s, std::streamsize num) override
int_type overflow(int_type ch) override
Keeps track of logging output to screen and/or files.
static void write(const char *s, std::streamsize num)
Print output message.
static void writeCh(char ch)
Print log message, single character.
static void flushDefault(bool error)
By default, flush std::cout or std::cerr.
static void writeDefault(const char *s, std::streamsize num, bool error)
By default, print to std::cout or std::cerr.
static void WriteFunThreadSafe(const char *s, std::streamsize num, bool error)
static void writeIgnore(const char *s, std::streamsize num, bool error)
Ignore output.
static void FlushThreadSafe(bool error)
The casadi namespace.
CASADI_EXPORT std::ostream & uout()
CASADI_EXPORT std::ostream & uerr()