Geogram  Version 1.9.1-rc
A programming library of geometric algorithms
logger.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2000-2022 Inria
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * * Neither the name of the ALICE Project-Team nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  * Contact: Bruno Levy
30  *
31  * https://www.inria.fr/fr/bruno-levy
32  *
33  * Inria,
34  * Domaine de Voluceau,
35  * 78150 Le Chesnay - Rocquencourt
36  * FRANCE
37  *
38  */
39 
40 #ifndef GEOGRAM_BASIC_LOGGER
41 #define GEOGRAM_BASIC_LOGGER
42 
43 #ifdef __cplusplus
44 
45 #include <geogram/basic/common.h>
47 #include <geogram/basic/process.h>
48 #include <iostream>
49 #include <fstream>
50 #include <sstream>
51 #include <string>
52 #include <set>
53 #include <stdlib.h>
54 
60 namespace GEO {
61 
62  class Logger;
63  class LoggerStream;
64 
73  class NO_GEOGRAM_API LoggerStreamBuf : public std::stringbuf {
74  public:
81  LoggerStreamBuf(LoggerStream* loggerStream) :
82  loggerStream_(loggerStream) {
83  }
84 
85  private:
94  int sync() override;
95 
96  private:
97  LoggerStream* loggerStream_;
98  };
99 
100  /************************************************************************/
101 
110  class NO_GEOGRAM_API LoggerStream : public std::ostream {
111  public:
118 
122  ~LoggerStream() override;
123 
124  protected:
134  void notify(const std::string& str);
135 
136  private:
137  Logger* logger_;
138  friend class LoggerStreamBuf;
139  };
140 
141  /************************************************************************/
142 
156  class GEOGRAM_API LoggerClient : public Counted {
157  public:
163  virtual void div(const std::string& title) = 0;
164 
169  virtual void out(const std::string& str) = 0;
170 
175  virtual void warn(const std::string& str) = 0;
176 
181  virtual void err(const std::string& str) = 0;
182 
187  virtual void status(const std::string& str) = 0;
188 
192  ~LoggerClient() override;
193  };
194 
197 
198  /************************************************************************/
199 
203  class GEOGRAM_API ConsoleLogger : public LoggerClient {
204  public:
209 
213  void div(const std::string& title) override;
214 
218  void out(const std::string& str) override;
219 
223  void warn(const std::string& str) override;
224 
228  void err(const std::string& str) override;
229 
234  void status(const std::string& str) override;
235 
236  protected:
240  ~ConsoleLogger() override;
241  };
242 
243  /************************************************************************/
244 
248  class GEOGRAM_API FileLogger : public LoggerClient {
249  public:
256 
263  FileLogger(const std::string& file_name);
264 
268  void div(const std::string& title) override;
269 
273  void out(const std::string& str) override;
274 
278  void warn(const std::string& str) override;
279 
283  void err(const std::string& str) override;
284 
289  void status(const std::string& str) override;
290 
291  protected:
295  ~FileLogger() override;
296 
303  void set_file_name(const std::string& file_name);
304 
305  private:
306  std::string log_file_name_;
307  std::ostream* log_file_;
308  };
309 
310  /************************************************************************/
311 
358  class GEOGRAM_API Logger : public Environment {
359  public:
367  static void initialize();
368 
375  static void terminate();
376 
387  static Logger* instance();
388 
389 
399  static bool is_initialized();
400 
401 
413  static std::ostream& div(const std::string& title);
414 
425  static std::ostream& out(const std::string& feature);
426 
437  static std::ostream& err(const std::string& feature);
438 
449  static std::ostream& warn(const std::string& feature);
450 
459  static std::ostream& status();
460 
471 
482 
487 
494  bool is_client(LoggerClient* client) const;
495 
506  void set_quiet(bool flag);
507 
513  bool is_quiet() const {
514  return quiet_;
515  }
516 
517 
528  void set_minimal(bool flag);
529 
535  bool is_minimal() const {
536  return minimal_;
537  }
538 
549  void set_pretty(bool flag);
550 
556  bool is_pretty() const {
557  return pretty_;
558  }
559 
560  protected:
567 
571  ~Logger() override;
572 
574  std::ostream& div_stream(const std::string& title);
575 
577  std::ostream& out_stream(const std::string& feature);
578 
580  std::ostream& err_stream(const std::string& feature);
581 
583  std::ostream& warn_stream(const std::string& feature);
584 
586  std::ostream& status_stream();
587 
595  std::ostream& err_console();
596 
611  void notify(LoggerStream* sender, const std::string& message);
612 
624  void notify_out(const std::string& message);
625 
635  void notify_warn(const std::string& message);
636 
646  void notify_err(const std::string& message);
647 
657  void notify_status(const std::string& message);
658 
672  const std::string& name, const std::string& value
673  ) override;
674 
687  const std::string& name, std::string& value
688  ) const override;
689 
690  private:
691  static SmartPointer<Logger> instance_;
692 
693  LoggerStream out_;
694  LoggerStream warn_;
695  LoggerStream err_;
696  LoggerStream status_;
697 
698  std::ostream* err_console_;
699 
700  // features we want or don't want to log (only applies to 'out').
701 
703  typedef std::set<std::string> FeatureSet;
704  FeatureSet log_features_;
705  FeatureSet log_features_exclude_;
706  bool log_everything_;
707  std::string log_file_name_;
708 
709  std::string current_feature_;
710  bool current_feature_changed_;
711 
713  typedef std::set<LoggerClient_var> LoggerClients;
714  LoggerClients clients_; // list of registered clients
715 
716  bool quiet_;
717  bool pretty_;
718  bool minimal_;
719  bool notifying_error_;
720 
721  friend class LoggerStream;
722  friend class LoggerStreamBuf;
723  };
724 
725  /************************************************************************/
726 
727 }
728 
729 extern "C" {
738  int GEOGRAM_API geogram_printf(const char* format, ...);
739 
753  int GEOGRAM_API geogram_fprintf(FILE* out, const char* format, ...);
754 }
755 
756 #else
757 
758 #include <stdlib.h>
759 
760 #ifndef GEOGRAM_API
761 #define GEOGRAM_API
762 #endif
763 
772 extern int GEOGRAM_API geogram_printf(const char* format, ...);
773 
786 extern int GEOGRAM_API geogram_fprintf(FILE* out, const char* format, ...);
787 
788 #endif
789 
790 #endif
Logger client that redirects messages to standard output.
Definition: logger.h:203
void warn(const std::string &str) override
Handles a warning message.
void div(const std::string &title) override
Creates a new division.
void status(const std::string &str) override
Handles a status message.
~ConsoleLogger() override
ConsoleLogger destructor.
ConsoleLogger()
Creates a ConsoleLogger.
void out(const std::string &str) override
Handles an information message.
void err(const std::string &str) override
Handles an error message.
Base class for reference-counted objects.
Definition: counted.h:71
Application environment.
Definition: environment.h:211
Logger client that redirects messages to a file.
Definition: logger.h:248
FileLogger(const std::string &file_name)
Creates logger that logs messages to a file.
void set_file_name(const std::string &file_name)
Sets the log file name.
void out(const std::string &str) override
Handles an information message.
void err(const std::string &str) override
Handles an error message.
FileLogger()
Creates an empty file logger.
void warn(const std::string &str) override
Handles a warning message.
~FileLogger() override
FileLogger destructor.
void status(const std::string &str) override
Handles a status message.
void div(const std::string &title) override
Creates a new division.
Logger client base class.
Definition: logger.h:156
virtual void div(const std::string &title)=0
Creates a new division.
virtual void status(const std::string &str)=0
Handles a status message.
virtual void err(const std::string &str)=0
Handles an error message.
virtual void warn(const std::string &str)=0
Handles a warning message.
virtual void out(const std::string &str)=0
Handles an information message.
~LoggerClient() override
LoggerClient destructor.
Stream buffer used by the LoggerStreams.
Definition: logger.h:73
LoggerStreamBuf(LoggerStream *loggerStream)
Creates a Logger stream buffer.
Definition: logger.h:81
Stream used by the Logger.
Definition: logger.h:110
~LoggerStream() override
Logger stream destructor.
LoggerStream(Logger *logger)
Creates a Logger stream.
void notify(const std::string &str)
Sends a string to the Logger.
Generic logging framework.
Definition: logger.h:358
std::ostream & div_stream(const std::string &title)
Creates a division in the log output.
static std::ostream & warn(const std::string &feature)
Gets the stream to send warning messages.
void set_minimal(bool flag)
Sets the minimal mode.
void unregister_client(LoggerClient *client)
Removes a client from the Logger.
~Logger() override
Logger destructor.
static std::ostream & div(const std::string &title)
Creates a division in the log output.
static void initialize()
Initializes the logging system.
void set_quiet(bool flag)
Sets the quiet mode.
static void terminate()
Terminates the logging system.
std::ostream & status_stream()
Gets the stream to send status messages.
bool is_pretty() const
Checks the console pretty mode.
Definition: logger.h:556
static bool is_initialized()
Tests whether the Logger is initialized.
std::ostream & warn_stream(const std::string &feature)
Gets the stream to send warning messages.
std::ostream & err_stream(const std::string &feature)
Gets the stream to send error messages.
bool is_client(LoggerClient *client) const
Checks if a client is registered.
void set_pretty(bool flag)
Sets the console pretty mode.
Logger()
Logger default constructor.
void notify_warn(const std::string &message)
Handles a warning message.
bool get_local_value(const std::string &name, std::string &value) const override
Gets a Logger property.
void unregister_all_clients()
Unregisters all the registered clients.
bool is_minimal() const
Checks the minimal mode.
Definition: logger.h:535
bool is_quiet() const
Checks the quiet mode.
Definition: logger.h:513
void notify_err(const std::string &message)
Handles an error message.
void register_client(LoggerClient *client)
Adds a client to the Logger.
std::ostream & out_stream(const std::string &feature)
Gets the stream to send information messages.
static Logger * instance()
Returns the Logger single instance.
std::ostream & err_console()
Gets an output stream that sends messages to the standard error.
void notify_out(const std::string &message)
Handles an information message.
bool set_local_value(const std::string &name, const std::string &value) override
Sets a Logger property.
void notify_status(const std::string &message)
Handles a status message.
void notify(LoggerStream *sender, const std::string &message)
Receives a message from a logger stream.
static std::ostream & err(const std::string &feature)
Gets the stream to send error messages.
static std::ostream & status()
Gets the stream to send status messages.
static std::ostream & out(const std::string &feature)
Gets the stream to send information messages.
A smart pointer with reference-counted copy semantics.
Definition: smart_pointer.h:76
Provides a mechanism to store global variables, retrieve them by their names and attach observers to ...
#define NO_GEOGRAM_API
A place-holder linkage declaration to indicate that the symbol should not be exported by Windows DLLs...
Definition: defs.h:93
Common include file, providing basic definitions. Should be included before anything else by all head...
int geogram_fprintf(FILE *out, const char *format,...)
Fprintf-like wrapper to the Logger.
int geogram_printf(const char *format,...)
Printf-like wrapper to the Logger.
Global Vorpaline namespace.
Definition: basic.h:55
SmartPointer< LoggerClient > LoggerClient_var
Definition: logger.h:196
Function and classes for process manipulation.