Branch data Line data Source code
1 : : // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
2 : : //
3 : : // Permission to use, copy, modify, and/or distribute this software for any
4 : : // purpose with or without fee is hereby granted, provided that the above
5 : : // copyright notice and this permission notice appear in all copies.
6 : : //
7 : : // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
8 : : // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
9 : : // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
10 : : // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
11 : : // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
12 : : // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
13 : : // PERFORMANCE OF THIS SOFTWARE.
14 : :
15 : : #include "config.h"
16 : : #include <log/log_formatter.h>
17 : :
18 : : #include <cassert>
19 : :
20 : : using namespace std;
21 : : using namespace boost;
22 : :
23 : : namespace isc {
24 : : namespace log {
25 : :
26 : : void
27 : 100404 : replacePlaceholder(string* message, const string& arg,
28 : : const unsigned placeholder)
29 : : {
30 [ + - ]: 200808 : const string mark("%" + lexical_cast<string>(placeholder));
31 [ + - ]: 100404 : size_t pos(message->find(mark));
32 [ + + ]: 100404 : if (pos != string::npos) {
33 [ + + ]: 99234 : do {
34 [ + - ]: 99234 : message->replace(pos, mark.size(), arg);
35 [ + - ]: 99234 : pos = message->find(mark, pos + arg.size());
36 : : } while (pos != string::npos);
37 : : }
38 : : #ifdef ENABLE_LOGGER_CHECKS
39 : : else {
40 : : // We're missing the placeholder, so throw an exception
41 : : isc_throw(MismatchedPlaceholders,
42 : : "Missing logger placeholder in message: " << *message);
43 : : }
44 : : #else
45 : : else {
46 : : // We're missing the placeholder, so add some complain
47 [ + - ][ + - ]: 2358 : message->append(" @@Missing placeholder " + mark + " for '" + arg + "'@@");
[ + - ][ + - ]
48 : : }
49 : : #endif /* ENABLE_LOGGER_CHECKS */
50 : 100404 : }
51 : :
52 : : void
53 : 40803 : checkExcessPlaceholders(string* message, unsigned int placeholder) {
54 [ + - ]: 81606 : const string mark("%" + lexical_cast<string>(placeholder));
55 [ + - ]: 40803 : const size_t pos(message->find(mark));
56 [ + + ]: 40803 : if (pos != string::npos) {
57 : : // Excess placeholders were found. If we enable the harsh check,
58 : : // abort it. Note: ideally we'd like to throw MismatchedPlaceholders,
59 : : // but we can't at least for now because this function is called from
60 : : // the Formatter's destructor.
61 : : #ifdef ENABLE_LOGGER_CHECKS
62 : : assert("Excess logger placeholders still exist in message" == NULL);
63 : : #else
64 [ + - ]: 3 : message->append(" @@Excess logger placeholders still exist@@");
65 : : #endif /* ENABLE_LOGGER_CHECKS */
66 : : }
67 : 40803 : }
68 : :
69 : : }
70 : 141 : }
|