Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
212 | dhylands | 1 | /**************************************************************************** |
2 | * |
||
3 | * Copyright (c) 2003 Dave Hylands |
||
4 | * All Rights Reserved |
||
5 | * |
||
6 | * Permission is granted to any individual or institution to use, copy, or |
||
7 | * redistribute this software so long as it is not sold for profit, and that |
||
8 | * this copyright notice is retained. |
||
9 | * |
||
10 | ****************************************************************************/ |
||
11 | /** |
||
12 | * |
||
13 | * @file LogMessage.cpp |
||
14 | * |
||
15 | * @brief Implements the message logging infrastructure. |
||
16 | * |
||
17 | ****************************************************************************/ |
||
18 | |||
19 | // ---- Include Files ------------------------------------------------------- |
||
20 | |||
21 | #include "LogMessage.h" |
||
22 | #include <sstream> |
||
23 | |||
24 | // ---- Public Variables ---------------------------------------------------- |
||
25 | // ---- Private Constants and Types ----------------------------------------- |
||
26 | // ---- Private Variables --------------------------------------------------- |
||
27 | // ---- Private Function Prototypes ----------------------------------------- |
||
28 | // ---- Functions ----------------------------------------------------------- |
||
29 | |||
30 | /** |
||
31 | * @addtogroup Log |
||
32 | * @{ |
||
33 | */ |
||
34 | |||
35 | /** |
||
36 | * Variable which contains the handler that performs the real logging. |
||
37 | */ |
||
38 | LogMessage::Handler LogMessage::m_handler = NULL; |
||
39 | |||
40 | //*************************************************************************** |
||
41 | /** |
||
42 | * Function that causes the actual message to be logged. |
||
43 | */ |
||
44 | |||
45 | void LogMessage::Log |
||
46 | ( |
||
47 | LogMessage::Severity sev, ///< Message severity |
||
48 | const char *file, ///< Filename where log occurs |
||
49 | int lineNum ///< Line number where log occurs |
||
50 | ) |
||
51 | { |
||
52 | if ( m_handler != NULL ) |
||
53 | { |
||
54 | std::string str = m_stream.str(); |
||
55 | |||
56 | m_handler( sev, str.c_str(), file, lineNum ); |
||
57 | } |
||
58 | |||
59 | } // Log |
||
60 | |||
61 | //*************************************************************************** |
||
62 | /** |
||
63 | * Printf function that accumulates data until a newline is encountered. |
||
64 | * |
||
65 | * static |
||
66 | */ |
||
67 | |||
68 | void LogMessage::PrintStr |
||
69 | ( |
||
70 | LogMessage::Severity sev, ///< Message severity |
||
71 | const char *str ///< String to print |
||
72 | ) |
||
73 | { |
||
74 | if ( m_handler == NULL ) |
||
75 | { |
||
76 | return; |
||
77 | } |
||
78 | |||
79 | static char line[ 512 ] = { 0 }; |
||
80 | char *endLine = &line[ sizeof( line ) - 1]; |
||
81 | const char *src = str; |
||
82 | |||
83 | do |
||
84 | { |
||
85 | int lineLen = strlen( line ); |
||
86 | |||
87 | char *dst = &line[ lineLen ]; |
||
88 | |||
89 | while (( *src != '\0' ) && ( *src != '\n' ) && ( dst < endLine )) |
||
90 | { |
||
91 | *dst++ = *src++; |
||
92 | } |
||
93 | *dst = '\0'; |
||
94 | |||
95 | if (( *src == '\n' ) || ( dst >= endLine )) |
||
96 | { |
||
97 | // Don't use the LOG macro, since it may cause memory allocation |
||
98 | // to occur, and this routine is often called as part of the |
||
99 | // process of dumping memory leaks |
||
100 | |||
101 | m_handler( sev, line, __FILE__, __LINE__ ); |
||
102 | |||
103 | line[ 0 ] = '\0'; |
||
104 | |||
105 | if ( *src == '\n' ) |
||
106 | { |
||
107 | src++; |
||
108 | } |
||
109 | } |
||
110 | |||
111 | } while ( *src != '\0' ); |
||
112 | |||
113 | } // Printf |
||
114 | |||
115 | /** @} */ |
||
116 |