1
2//
3// This source file is part of appleseed.
4// Visit http://appleseedhq.net/ for additional information and resources.
5//
6// This software is released under the MIT license.
7//
8// Copyright (c) 2010-2013 Francois Beaune, Jupiter Jazz Limited
9// Copyright (c) 2014-2017 Francois Beaune, The appleseedhq Organization
10//
11// Permission is hereby granted, free of charge, to any person obtaining a copy
12// of this software and associated documentation files (the "Software"), to deal
13// in the Software without restriction, including without limitation the rights
14// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15// copies of the Software, and to permit persons to whom the Software is
16// furnished to do so, subject to the following conditions:
17//
18// The above copyright notice and this permission notice shall be included in
19// all copies or substantial portions of the Software.
20//
21// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27// THE SOFTWARE.
28//
29
30#ifndef APPLESEED_FOUNDATION_UTILITY_LOG_LOGGER_H
31#define APPLESEED_FOUNDATION_UTILITY_LOG_LOGGER_H
32
33// appleseed.foundation headers.
34#include "foundation/core/concepts/noncopyable.h"
35#include "foundation/platform/compiler.h"
36#include "foundation/utility/log/logmessage.h"
37
38// appleseed.main headers.
39#include "main/dllsymbol.h"
40
41// Standard headers.
42#include <cstddef>
43#include <string>
44
45// Forward declarations.
46namespace foundation { class ILogTarget; }
47
48namespace foundation
49{
50
51//
52// Logger.
53//
54// All methods of this class are thread-safe.
55//
56
57class APPLESEED_DLLSYMBOL Logger
58 : public NonCopyable
59{
60 public:
61 // Constructor.
62 Logger();
63
64 // Destructor.
65 virtual ~Logger();
66
67 // Copy all settings from a given logger to this one.
68 // Existing log targets are removed (but not deleted).
69 // Log targets of the source logger are added to this one.
70 // If the other logger is disabled, this one will be too.
71 void initialize_from(const Logger& source);
72
73 // Enable/disable logging.
74 void set_enabled(const bool enabled = true);
75
76 // Set/get the verbosity level.
77 void set_verbosity_level(const LogMessage::Category level);
78 LogMessage::Category get_verbosity_level() const;
79
80 // Reset the format string for all message categories.
81 void reset_all_formats();
82
83 // Reset the format string for a given message category.
84 void reset_format(const LogMessage::Category category);
85
86 // Set the format string for all message categories.
87 void set_all_formats(const char* format);
88 void set_all_formats(const std::string& format);
89
90 // Set the format string for a given message category.
91 void set_format(const LogMessage::Category category, const char* format);
92 void set_format(const LogMessage::Category category, const std::string& format);
93
94 // Return the format string of a given message category.
95 const char* get_format(const LogMessage::Category category) const;
96
97 // Add a log target. A given log target may be added
98 // multiple times. Log targets can be added at any time.
99 void add_target(ILogTarget* target);
100
101 // Remove all instances of a given log target.
102 // If the specified target cannot be found, nothing happens.
103 // Log targets can be removed at any time.
104 void remove_target(ILogTarget* target);
105
106 // Write a message. If the message category is Fatal,
107 // this function will not return and the program will
108 // be terminated.
109 void write(
110 const LogMessage::Category category,
111 const char* file,
112 const size_t line,
113 APPLESEED_PRINTF_FMT const char* format, ...)
114 APPLESEED_PRINTF_FMT_ATTR(5, 6);
115
116 private:
117 struct Impl;
118 Impl* impl;
119};
120
121
122//
123// Logger class implementation.
124//
125
126inline void Logger::set_all_formats(const std::string& format)
127{
128 set_all_formats(format.c_str());
129}
130
131inline void Logger::set_format(const LogMessage::Category category, const std::string& format)
132{
133 set_format(category, format.c_str());
134}
135
136} // namespace foundation
137
138#endif // !APPLESEED_FOUNDATION_UTILITY_LOG_LOGGER_H
139