Geogram Version 1.9.6-rc
A programming library of geometric algorithms
Loading...
Searching...
No Matches
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
48#include <iostream>
49#include <fstream>
50#include <sstream>
51#include <string>
52#include <set>
53#include <stdlib.h>
54
60namespace GEO {
61
62 class Logger;
63 class LoggerStream;
64
73 class NO_GEOGRAM_API LoggerStreamBuf : public std::stringbuf {
74 public:
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
695 void indent() {
696 ++indent_;
697 }
698
703 void unindent() {
704 geo_debug_assert(indent_ != 0);
705 --indent_;
706 }
707
708 private:
709 static SmartPointer<Logger> instance_;
710
711 LoggerStream out_;
712 LoggerStream warn_;
713 LoggerStream err_;
714 LoggerStream status_;
715
716 std::ostream* err_console_;
717
718 // features we want or don't want to log (only applies to 'out').
719
721 typedef std::set<std::string> FeatureSet;
722 FeatureSet log_features_;
723 FeatureSet log_features_exclude_;
724 bool log_everything_;
725 std::string log_file_name_;
726
727 std::string current_feature_;
728 bool current_feature_changed_;
729
731 typedef std::set<LoggerClient_var> LoggerClients;
732 LoggerClients clients_; // list of registered clients
733
734 bool quiet_;
735 bool pretty_;
736 bool minimal_;
737 bool notifying_error_;
738
739 index_t indent_;
740
741 friend class LoggerStream;
742 friend class LoggerStreamBuf;
743 friend class Stopwatch;
744 };
745
746 /************************************************************************/
747
748}
749
750extern "C" {
759 int GEOGRAM_API geogram_printf(const char* format, ...);
760
774 int GEOGRAM_API geogram_fprintf(FILE* out, const char* format, ...);
775}
776
777#else
778
779#include <stdlib.h>
780
781#ifndef GEOGRAM_API
782#define GEOGRAM_API
783#endif
784
793extern int GEOGRAM_API geogram_printf(const char* format, ...);
794
807extern int GEOGRAM_API geogram_fprintf(FILE* out, const char* format, ...);
808
809#endif
810
811#endif
#define geo_debug_assert(x)
Verifies that a condition is met.
Definition assert.h:196
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.
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
static std::ostream & out(const std::string &feature)
Gets the stream to send information messages.
void set_minimal(bool flag)
Sets the minimal mode.
std::ostream & err_stream(const std::string &feature)
std::ostream & div_stream(const std::string &title)
void unregister_client(LoggerClient *client)
Removes a client from the Logger.
~Logger() override
Logger destructor.
static void initialize()
Initializes the logging system.
std::ostream & out_stream(const std::string &feature)
void set_quiet(bool flag)
Sets the quiet mode.
static void terminate()
Terminates the logging system.
bool is_pretty() const
Checks the console pretty mode.
Definition logger.h:556
static bool is_initialized()
Tests whether the Logger is initialized.
bool is_client(LoggerClient *client) const
Checks if a client is registered.
void set_pretty(bool flag)
Sets the console pretty mode.
static std::ostream & warn(const std::string &feature)
Gets the stream to send warning messages.
static std::ostream & div(const std::string &title)
Creates a division in the log output.
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.
static std::ostream & err(const std::string &feature)
Gets the stream to send error messages.
std::ostream & warn_stream(const std::string &feature)
void indent()
Increases number of spaces before each message in out().
Definition logger.h:695
void notify_out(const std::string &message)
Handles an information message.
void unindent()
Decreases number of spaces before each message in out().
Definition logger.h:703
bool set_local_value(const std::string &name, const std::string &value) override
Sets a Logger property.
static Logger * instance()
Returns the Logger single instance.
void notify_status(const std::string &message)
Handles a status message.
std::ostream & status_stream()
void notify(LoggerStream *sender, const std::string &message)
Receives a message from a logger stream.
static std::ostream & status()
Gets the stream to send status messages.
std::ostream & err_console()
Gets an output stream that sends messages to the standard error.
A smart pointer with reference-counted copy semantics.
Scope restricted stopwatch.
Definition stopwatch.h:69
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
geo_index_t index_t
The type for storing and manipulating indices.
Definition numeric.h:329
SmartPointer< LoggerClient > LoggerClient_var
Definition logger.h:196
Function and classes for process manipulation.