• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1[/
2              Copyright Andrey Semashev 2007 - 2015.
3     Distributed under the Boost Software License, Version 1.0.
4        (See accompanying file LICENSE_1_0.txt or copy at
5              http://www.boost.org/LICENSE_1_0.txt)
6
7    This document is a part of Boost.Log library documentation.
8/]
9
10[section:rationale Rationale and FAQ]
11
12[section:why_str_lit Why string literals as scope names?]
13
14One may wonder why not allow arbitrary strings to be used as named scope names. The answer is simple: for performance and safety reasons. Named scope support functionality has one significant difference from other attribute-related features of the library. The scope stack is maintained even when no logging is done, so if a function `foo` has a `BOOST_LOG_FUNCTION()` statement in its body, it is always a slowdown. Allowing the scope name to be an arbitrary string would make the slowdown significantly greater because of the need to allocate memory and copy the string (not to mention that there would be a need to previously format it, which also takes its toll).
15
16Dynamic memory allocation also introduces exception safety issues: the `BOOST_LOG_FUNCTION()` statement (and alikes) would become a potential source of exceptions. These issues would complicate user's code if he wants to solve memory allocation problems gracefully.
17
18One possible alternative solution would be pooling pre-formatted and pre-allocated scope names somewhere but this would surely degrade performance even more and introduce the problem of detecting when to update or free pooled strings.
19
20Therefore restricting to string literals seemed to be the optimal decision, which reduced dynamic memory usage and provided enough flexibility for common needs.
21
22[endsect]
23
24[section:why_weak_scoped_attributes Why scoped attributes don't override existing attributes?]
25
26Initially scoped attributes were able to override other attributes with the same name if they were already registered by the time when a scoped attribute encountered. This allowed some interesting use cases like this:
27
28    BOOST_LOG_DECLARE_GLOBAL_LOGGER(my_logger, src::logger_mt)
29
30    void foo()
31    {
32        // This scoped attribute would temporarily replace the existing tag
33        BOOST_LOG_SCOPED_THREAD_TAG("Section", std::string, "In foo");
34
35        // This log record will have a "Section" attribute with value "In foo"
36        BOOST_LOG(get_my_logger()) << "We're in foo section";
37    }
38
39    int main(int, char*[])
40    {
41        BOOST_LOG_SCOPED_THREAD_TAG("Section", std::string, "In main");
42
43        // This log record will have a "Section" attribute with value "In main"
44        BOOST_LOG(get_my_logger()) << "We're in main section";
45
46        foo();
47
48        // This log record will have a "Section" attribute with value "In main" again
49        BOOST_LOG(get_my_logger()) << "We're in main section again";
50
51        return 0;
52    }
53
54However, this feature introduced a number of safety problems, including thread safety issues, that could be difficult to track down. For example, it was no longer safe to use logger-wide scoped attributes on the same logger from different threads, because the resulting attribute would be undefined:
55
56    BOOST_LOG_DECLARE_GLOBAL_LOGGER(my_logger, src::logger_mt)
57
58    void thread1()
59    {
60        BOOST_LOG_SCOPED_LOGGER_TAG(get_my_logger(), "Tag", std::string, "thread1");
61        BOOST_LOG(get_my_logger()) << "We're in thread1";
62    }
63
64    void thread2()
65    {
66        BOOST_LOG_SCOPED_LOGGER_TAG(get_my_logger(), "Tag", int, 10);
67        BOOST_LOG(get_my_logger()) << "We're in thread2";
68    }
69
70    int main(int, char*[])
71    {
72        BOOST_LOG_SCOPED_LOGGER_TAG(get_my_logger(), "Tag", double, -2.2);
73
74        BOOST_LOG(get_my_logger()) << "We're in main";
75
76        boost::thread t1(&thread1);
77        boost::thread t2(&thread2);
78
79        t1.join();
80        t2.join();
81
82        // Which "Tag" is registered here?
83        BOOST_LOG(get_my_logger()) << "We're in main again";
84
85        return 0;
86    }
87
88There were other issues, like having an attribute set iterator that points to one attribute object, then suddenly without seemingly modifying it it becomes pointing to a different attribute object (of, possibly, a different type). Such behavior could lead to tricky failures that would be difficult to investigate. Therefore this feature was eventually dropped, which simplified the scoped attributes implementation significantly.
89
90[endsect]
91
92[section:why_weak_record_ordering Why log records are weakly ordered in a multithreaded application?]
93
94Although the library guarantees that log records made in a given thread are always delivered to sinks in the same order as they were made in, the library cannot provide such guarantee for different threads. For instance, it is possible that thread A emits a log record and gets preempted, then thread B emits its log record and manages to deliver it to a sink before being preempted. The resulting log will contain log record from thread B before the record made in thread A. However, attribute values attached to the records will always be actual with regard to the moment of emitting the record and not the moment of passing the record to the sink. This is the reason for a strange, at first glance, situation when a log record with an earlier time stamp follows a record with a later time stamp. The problem appears quite rarely, usually when thread contention on logging is very high.
95
96There are few possible ways to cope with the problem:
97
98* Enforce strict serialization of log record being made throughout the application. This solution implies a severe performance impact in multithreaded applications because log records that otherwise could be processed concurrently would have to go serial. Since this controverses one of the [link log.moti main library goals], it was rejected.
99* Attempt to maintain log record ordering on the sink level. This solution is more or less viable. On the downside, it would introduce log record buffering, which in turn would compromise logs reliability. In the case of application crash all buffered records would be lost.
100* Bear with the problem and let mis-ordered records appear in log files occasionally. Order log records upon reading the files, if needed.
101
102The second solution was implemented as a special policy for the [link log.detailed.sink_frontends.async asynchronous sink frontend].
103
104[endsect]
105
106[section:why_attribute_manips_dont_affect_filters Why attributes set with stream manipulators do not participate in filtering?]
107
108One can add attributes to log records in the following way:
109
110    BOOST_LOG(logger) << logging::add_value("MyInt", 10) << logging::add_value("MyString", "string attribute value")
111        << "Some log message";
112
113However, filters will not be able to use MyInt and MyString attributes. The reason for this behavior is quite simple. The streaming expression is executed /after/ the filtering takes place and only /if/ the filter passed the log record. At this point these attributes have not been added to the record yet. The easiest way to pass attributes to the filter is to use scoped attributes or tags (see [link log.detailed.attributes.related_components.scoped_attributes here]).
114
115[endsect]
116
117[section:why_not_lazy_streaming Why not using lazy streaming?]
118
119One of the possible library implementations would be using lazy expressions to delay log record formatting. In essence, the expression:
120
121    logger << "Hello, world!";
122
123would become a lambda-expression that is only invoked if the filtering is successful. Although this approach has advantages, it must be noted that lazy expression construction is not zero-cost in terms of performance, code size and compile times. The following expression:
124
125    logger << "Received packet from " << ip << " of " << packet.size() << " bytes";
126
127would generate a considerable amount of code (proportional to the number of streaming operators) to be executed before filtering takes place. Another drawback is that the `packet.size()` is always called, whether or not the record is actually written to the log. In order to delay this call, yet more scaffolding is needed, possibly involving __boost_bind__, __boost_lambda__ or __boost_phoenix__. This complication is not acceptable for such a basic use case, like this.
128
129Although lazy streaming is not provided by the library out of the box, nothing prevents developing it in a separate hierarchy of loggers. See the [link log.extension.sources Extending the library] section for more information.
130
131[endsect]
132
133[section:why_not_log4j Why not using hierarchy of loggers, like in log4j? Why not Boost.Log4j? Etc.]
134
135There are enough [@http://logging.apache.org/log4j/ log4j]-like libraries available for C++ already (see [@http://logging.apache.org/log4cxx/ here], [@http://log4cplus.sourceforge.net/ here] and [@http://log4cpp.sourceforge.net/ here]), so there is no point in implementing yet another one. Instead, this library was aimed to solve more complex tasks, including ones that do not directly fall under the common definition of "logging" term as a debugging tool. Additionally, as Boost.Log was to be a generic library, it had to provide more ways of extending itself, while keeping performance as high as possible. Log4j concept seemed too limiting and inappropriate for these tasks and therefore was rejected.
136
137As for hierarchical loggers, there is no need for this feature in the current library design. One of the main benefits it provides in log4j is determining the appenders (sinks, in terms of this library) in which a log record will end up. This library achieves the same result by filtering. The other application of this feature in Boost.Log could be that the loggers in the hierarchy could combine their sets of attributes for each log record, but there was no demand in real world applications for such a feature. It can be added though, if it proves useful.
138
139[endsect]
140
141[section:fork_support Does Boost.Log support process forking?]
142
143No, currently Boost.Log does not support process forking (i.e. `fork` call in UNIX systems). There are several issues with process forking, for instance:
144
145* File sinks do not attempt to reopen log files or synchronize access to files between parent and child processes. The resulting output may be garbled.
146* File collectors do not expect several processes attempting to collect log files to the same target directory. This may result in spurious failures at log file rotation.
147* The [link log.detailed.attributes.process_id current_process_id] attribute value will not update in the child process.
148* In multithreaded applications, one can generally not guarantee that a thread is not executing some Boost.Log code while an other thread forks. Some Boost.Log resources may be left irreversibly locked or broken in the forked process. This reservation is not specific to Boost.Log, other libraries and even the application itself are susceptible to this problem.
149
150There may be other issues as well. It seems unlikely that support for forking will be added to Boost.Log any time soon.
151
152[note This does not preclude the `fork`+`exec` sequence from working. As long as the forked process doesn't try to use any of Boost.Log code, the process should be able to call `exec` or a similar function to load and start another executable.]
153
154[endsect]
155
156[section:init_term_support Does Boost.Log support logging at process initialization and termination?]
157
158It should be fine to use logging during the application initialization (i.e. before `main()` starts). But there are a number of known problems with Boost.Log that prevent it from being used at process termination (i.e. after the `main()` function returns), so the official answer to the second part is no. It may work though, in some very restricted setups, if several rules are followed:
159
160* Do not create any objects at process termination, including loggers, attributes or sinks. Try to create and cache the required objects as soon as the application starts (maybe even before `main()` starts).
161* Do not use global loggers at process termination.
162* Do not call `logging::core::get()` at process termination. Get that pointer as early as possible and keep it until the process terminates.
163* Do not use named scopes in termination code.
164
165These rules don't guarantee that the library will work in termination context but they may help to avoid problems. The library will get improved to support this use case better.
166
167[endsect]
168
169[section:why_crash_on_term Why my application crashes on process termination when file sinks are used?]
170
171There are known problems with __boost_filesystem__ (for example, [ticket 8642] and [ticket 9219]), which affect Boost.Log file sink backends. When the file sink is destroyed, it attempts to perform a final log file rotation, which involves __boost_filesystem__ for moving files. This typically happens when Boost.Log core is deinitialized, at the global deinitialization stage, after leaving `main()`. The crux of the problem is that __boost_filesystem__ uses a global locale object internally to perform character code conversion for `path`s, and this locale may get destroyed before Boost.Log is deinitialized, which results in a crash.
172
173There is no way for Boost.Log to influence the order of global deinitialization, but the problem can be worked around on the user's side. One solution is to make sure the locale is initialized /before/ Boost.Log. This can be achieved by calling `boost::filesystem::path::codecvt()` or `boost::filesystem::path::imbue()` early during the application startup, before performing any calls to Boost.Log. For example:
174
175    int main(int argc, char* argv[])
176    {
177        boost::filesystem::path::imbue(std::locale("C"));
178        initialize_log();
179
180        // ...
181    }
182
183Note that in this case you can't use Boost.Log in global constructors or you have to make sure that `boost::filesystem::path::imbue()` is still called first.
184
185Another solution is to remove and destroy file sinks from the logging core before returning from `main()`. This way file rotation will happen before leaving `main()`, while the locale is still valid. The file sinks can be removed either individually or as a part of the `remove_all_sinks()` call:
186
187    int main(int argc, char* argv[])
188    {
189        // ...
190
191        logging::core::get()->remove_all_sinks();
192
193        return 0;
194    }
195
196Lastly, you can disable the final log file rotation in every file sink you register in the logging core. For sinks added programmatically this can be done by calling `enable_final_rotation(false)` on the sink backend. If the sink is created from [link log.detailed.utilities.setup.settings settings], you can do this by setting EnableFinalRotation parameter to "false".
197
198[endsect]
199
200[section:namespace_mangling Why my application fails to link with Boost.Log? What's the fuss about library namespaces?]
201
202The library declares the `boost::log` namespace which should be used in client code to access library components. However, internally the library uses another nested namespace for actual implementation. The namespace name is configuration and platform dependent, it can change between different releases of the library, so it should never be used in the user side code. This is done in order to make the library configuration synchronized with the application as much as possible and eliminate problems caused by configuration mismatch.
203
204Most of the time users won't even notice the existence of this internal namespace, but it often appears in compiler and linker errors and in some cases it is useful to know how to decode its name. Currently, the namespace name is composed from the following elements:
205
206[pre <version><linkage>\_<threading>\_<system>]
207
208* The `<version>` component describes the library major version. It is currently `v2`.
209* The `<linkage>` component tells whether the library is linked statically or dynamically. It is `s` if the library is linked statically and empty otherwise.
210* The `<threading>` component is `st` for single-threaded builds and `mt` for multi-threaded ones.
211* The `<system>` component describes the underlying OS API used by the library. Currently, it is only specified for multi-threaded builds. Depending on the target platform and configuration, it can be `posix`, `nt5` or `nt6`.
212
213As a couple quick examples, `v2s_st` corresponds to v2 static single-threaded build of the library and `v2_mt_posix` - to v2 dynamic multi-threaded build for POSIX system API.
214
215Namespace mangling may lead to linking errors if the application is misconfigured. One common mistake is to build dynamic version of the library and not define `BOOST_LOG_DYN_LINK` or `BOOST_ALL_DYN_LINK` when building the application, so that the library assumes static linking by default. Whenever such linking errors appear, one can decode the namespace name in the missing symbols and the exported symbols of Boost.Log library and adjust library or application [link log.installation.config configuration] accordingly.
216
217[endsect]
218
219[section:msvc_link_fails_lnk1123 Why MSVC 2010 fails to link the library with error LNK1123: failure during conversion to COFF: file invalid or corrupt?]
220
221If you have several versions of Visual Studio installed and trying to build the library with Visual Studio 2010, the compilation may fail with linker error LNK1123. This seems to be a [@http://stackoverflow.com/questions/10888391/link-fatal-error-lnk1123-failure-during-conversion-to-coff-file-invalid-or-c known problem] caused by some conflict between Visual Studio 2010 and .NET Framework 4.5, which is installed with Visual Studio 2012.
222
223The suggested solution is to upgrade Visual Studio 2010 to Visual Studio 2010 SP1 or overwrite [^"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\cvtres.exe"] with the one of Visual Studio 2010 SP1 or 2012.
224
225[endsect]
226
227[endsect]
228