|
| 1 | +// |
| 2 | +// |
| 3 | +// LCLLogFile.h |
| 4 | +// |
| 5 | +// |
| 6 | +// Copyright (c) 2008-2013 Arne Harren <ah@0xc0.de> |
| 7 | +// |
| 8 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | +// of this software and associated documentation files (the "Software"), to deal |
| 10 | +// in the Software without restriction, including without limitation the rights |
| 11 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | +// copies of the Software, and to permit persons to whom the Software is |
| 13 | +// furnished to do so, subject to the following conditions: |
| 14 | +// |
| 15 | +// The above copyright notice and this permission notice shall be included in |
| 16 | +// all copies or substantial portions of the Software. |
| 17 | +// |
| 18 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | +// THE SOFTWARE. |
| 25 | + |
| 26 | +#define _LCLLOGFILE_VERSION_MAJOR 1 |
| 27 | +#define _LCLLOGFILE_VERSION_MINOR 2 |
| 28 | +#define _LCLLOGFILE_VERSION_BUILD 3 |
| 29 | +#define _LCLLOGFILE_VERSION_SUFFIX "" |
| 30 | + |
| 31 | +// |
| 32 | +// LCLLogFile |
| 33 | +// |
| 34 | +// LCLLogFile is a logging back-end implementation which writes log messages to |
| 35 | +// an application-specific log file. LCLLogFile can be used as a logging |
| 36 | +// back-end for LibComponentLogging, but it is also useable as a standalone |
| 37 | +// logging class without the Core files of LibComponentLogging. |
| 38 | +// |
| 39 | +// The log file is opened automatically when the first log message needs to be |
| 40 | +// written to the log file. There is no need to call open, close, reset, etc. |
| 41 | +// manually. |
| 42 | +// |
| 43 | +// The log file gets rotated if a given maximum file size is reached. |
| 44 | +// |
| 45 | +// LCLLogFile is configured via the following #defines in LCLLogFileConfig.h |
| 46 | +// (see #import below): |
| 47 | +// |
| 48 | +// - Full path of the log file (type NSString *) |
| 49 | +// #define _LCLLogFile_LogFilePath <definition> |
| 50 | +// |
| 51 | +// - Append to an existing log file on startup? (type BOOL) |
| 52 | +// #define _LCLLogFile_AppendToExistingLogFile |
| 53 | +// |
| 54 | +// - Maximum size of the log file in bytes (type size_t) |
| 55 | +// #define _LCLLogFile_MaxLogFileSizeInBytes <definition> |
| 56 | +// |
| 57 | +// - Mirror log messages to stderr? (type BOOL) |
| 58 | +// #define _LCLLogFile_MirrorMessagesToStdErr <definition> |
| 59 | +// |
| 60 | +// - Escape ('\\' and) '\n' line feed characters in log messages (type BOOL) |
| 61 | +// #define _LCLLogFile_EscapeLineFeeds <definition> |
| 62 | +// |
| 63 | +// - Maximum size of a log message in characters (type NSUInteger) |
| 64 | +// #define _LCLLogFile_MaxMessageSizeInCharacters <definition or 0> |
| 65 | +// |
| 66 | +// - Show file names in the log messages? (type BOOL) |
| 67 | +// #define _LCLLogFile_ShowFileNames <definition> |
| 68 | +// |
| 69 | +// - Show line numbers in the log messages? (type BOOL) |
| 70 | +// #define _LCLLogFile_ShowLineNumbers <definition> |
| 71 | +// |
| 72 | +// - Show function names in the log messages? (type BOOL) |
| 73 | +// #define _LCLLogFile_ShowFunctionNames <definition> |
| 74 | +// |
| 75 | +// |
| 76 | +// When using LCLLogFile as a back-end for LibComponentLogging, simply add an |
| 77 | +// #import "LCLLogFile.h" |
| 78 | +// statement to your lcl_config_logger.h file and use the LCLLogFileConfig.h |
| 79 | +// file for detailed configuration of the LCLLogFile class. |
| 80 | +// |
| 81 | + |
| 82 | + |
| 83 | +#import <Foundation/Foundation.h> |
| 84 | +#import "LCLLogFileConfig.h" |
| 85 | + |
| 86 | + |
| 87 | +@interface LCLLogFile : NSObject { |
| 88 | + |
| 89 | +} |
| 90 | + |
| 91 | + |
| 92 | +// |
| 93 | +// Logging methods. |
| 94 | +// |
| 95 | + |
| 96 | + |
| 97 | +// Writes the given log message to the log file. |
| 98 | ++ (void)logWithIdentifier:(const char *)identifier level:(uint32_t)level |
| 99 | + path:(const char *)path line:(uint32_t)line |
| 100 | + function:(const char *)function |
| 101 | + message:(NSString *)message; |
| 102 | + |
| 103 | +// Writes the given log message to the log file (format and va_list var args). |
| 104 | ++ (void)logWithIdentifier:(const char *)identifier level:(uint32_t)level |
| 105 | + path:(const char *)path line:(uint32_t)line |
| 106 | + function:(const char *)function |
| 107 | + format:(NSString *)format args:(va_list)args; |
| 108 | + |
| 109 | +// Writes the given log message to the log file (format and ... var args). |
| 110 | ++ (void)logWithIdentifier:(const char *)identifier level:(uint32_t)level |
| 111 | + path:(const char *)path line:(uint32_t)line |
| 112 | + function:(const char *)function |
| 113 | + format:(NSString *)format, ... __attribute__((format(__NSString__, 6, 7))); |
| 114 | + |
| 115 | + |
| 116 | +// |
| 117 | +// Configuration. |
| 118 | +// |
| 119 | + |
| 120 | + |
| 121 | +// Returns/sets the path of the log file. Setting the path implies a reset. |
| 122 | ++ (NSString *)path; |
| 123 | ++ (void)setPath:(NSString *)path; |
| 124 | + |
| 125 | +// Returns the path of the backup log file. |
| 126 | ++ (NSString *)path0; |
| 127 | + |
| 128 | +// Returns/sets whether log messages get appended to an existing log file on |
| 129 | +// startup. |
| 130 | ++ (BOOL)appendsToExistingLogFile; |
| 131 | ++ (void)setAppendsToExistingLogFile:(BOOL)value; |
| 132 | + |
| 133 | +// Returns/sets the maximum size of the log file (as defined by |
| 134 | +// _LCLLogFile_MaxLogFileSizeInBytes). |
| 135 | ++ (size_t)maxSize; |
| 136 | ++ (void)setMaxSize:(size_t)value; |
| 137 | + |
| 138 | +// Returns/sets whether log messages are mirrored to stderr. |
| 139 | ++ (BOOL)mirrorsToStdErr; |
| 140 | ++ (void)setMirrorsToStdErr:(BOOL)value; |
| 141 | + |
| 142 | +// Returns/sets whether ('\\' and) '\n' line feed characters are escaped in |
| 143 | +// log messages. |
| 144 | ++ (BOOL)escapesLineFeeds; |
| 145 | ++ (void)setEscapesLineFeeds:(BOOL)value; |
| 146 | + |
| 147 | +// Returns/sets the maximum size of a log message in characters (without |
| 148 | +// prefixes). The value 0 indicates that there is no maximum size for log |
| 149 | +// messages. |
| 150 | ++ (NSUInteger)maxMessageSize; |
| 151 | ++ (void)setMaxMessageSize:(NSUInteger)value; |
| 152 | + |
| 153 | +// Returns/sets whether file names are shown. |
| 154 | ++ (BOOL)showsFileNames; |
| 155 | ++ (void)setShowsFileNames:(BOOL)value; |
| 156 | + |
| 157 | +// Returns/sets whether line numbers are shown. |
| 158 | ++ (BOOL)showsLineNumbers; |
| 159 | ++ (void)setShowsLineNumbers:(BOOL)value; |
| 160 | + |
| 161 | +// Returns/sets whether function names are shown. |
| 162 | ++ (BOOL)showsFunctionNames; |
| 163 | ++ (void)setShowsFunctionNames:(BOOL)value; |
| 164 | + |
| 165 | + |
| 166 | +// |
| 167 | +// Status information and internals. |
| 168 | +// |
| 169 | + |
| 170 | + |
| 171 | +// Returns the current size of the log file. |
| 172 | ++ (size_t)size; |
| 173 | + |
| 174 | +// Opens the log file. |
| 175 | ++ (void)open; |
| 176 | + |
| 177 | +// Closes the log file. |
| 178 | ++ (void)close; |
| 179 | + |
| 180 | +// Resets the log file. This also deletes the existing log file. |
| 181 | ++ (void)reset; |
| 182 | + |
| 183 | +// Rotates the log file. |
| 184 | ++ (void)rotate; |
| 185 | + |
| 186 | + |
| 187 | +// |
| 188 | +// Methods for creating log file paths. |
| 189 | +// |
| 190 | + |
| 191 | + |
| 192 | +// Returns a default path for a log file which is based on the Info.plist |
| 193 | +// files which are associated with this class. The returned path has the form |
| 194 | +// ~/Library/Logs/<main>/<this>.log |
| 195 | +// where |
| 196 | +// <main> is the name (or identifier) of the application's main bundle, and |
| 197 | +// <this> is the name (or identifier) of the bundle to which this LCLLogFile |
| 198 | +// class belongs. |
| 199 | +// This method is a convenience method which calls defaultPathWithPathPrefix |
| 200 | +// with the prefix ~/Library/Logs and the given fallback path. |
| 201 | ++ (NSString *)defaultPathInHomeLibraryLogsOrPath:(NSString *)path; |
| 202 | + |
| 203 | +// Returns a default path for a log file which is based on the Info.plist |
| 204 | +// files which are associated with this class. The returned path has the form |
| 205 | +// <path>/<main>/<this>.log |
| 206 | +// where |
| 207 | +// <path> is the given path prefix, |
| 208 | +// <main> is the name (or identifier) of the application's main bundle, and |
| 209 | +// <this> is the name (or identifier) of the bundle to which this LCLLogFile |
| 210 | +// class belongs. |
| 211 | +// If the name or identifier cannot be retrieved from the main bundle, the |
| 212 | +// returned default path has the form |
| 213 | +// <path>/<this>/<this>.<pid>.log |
| 214 | +// where |
| 215 | +// <pid> is the current process id. |
| 216 | +// If the name or identifier cannot be retrieved from the bundle which |
| 217 | +// corresponds to this LCLLogFile class, or if the given path prefix <path> is |
| 218 | +// nil, the given fallback path is returned. |
| 219 | ++ (NSString *)defaultPathWithPathPrefix:(NSString *)pathPrefix |
| 220 | + orPath:(NSString *)path; |
| 221 | + |
| 222 | +// Returns a default path component for a log file which is based on the given |
| 223 | +// bundles' Info.plist files. The returned path has the form |
| 224 | +// <path>/<file>.log |
| 225 | +// where |
| 226 | +// <path> is the name (or identifier) of the given path bundle, and |
| 227 | +// <file> is the name (or identifier) of the given file bundle. |
| 228 | +// If the name or identifier cannot be retrieved from the path bundle, the |
| 229 | +// returned default path has the form |
| 230 | +// <file>/<file>.<pid>.log |
| 231 | +// where |
| 232 | +// <pid> is the current process id. |
| 233 | +// If the name or identifier cannot be retrieved from the file bundle, the |
| 234 | +// given fallback path component is returned. |
| 235 | ++ (NSString *)defaultPathComponentFromPathBundle:(NSBundle *)pathBundle |
| 236 | + fileBundle:(NSBundle *)fileBundle |
| 237 | + orPathComponent:(NSString *)pathComponent; |
| 238 | + |
| 239 | +// Returns the name from the given bundle's Info.plist file. If the name doesn't |
| 240 | +// exist, the bundle's identifier is returned. If the identifier doesn't exist, |
| 241 | +// the given fallback string is returned. |
| 242 | ++ (NSString *)nameOrIdentifierFromBundle:(NSBundle *)bundle |
| 243 | + orString:(NSString *)string; |
| 244 | + |
| 245 | + |
| 246 | +@end |
| 247 | + |
| 248 | + |
| 249 | +// |
| 250 | +// Integration with LibComponentLogging Core. |
| 251 | +// |
| 252 | + |
| 253 | + |
| 254 | +// ARC/non-ARC autorelease pool |
| 255 | +#define _lcl_logger_autoreleasepool_arc 0 |
| 256 | +#if defined(__has_feature) |
| 257 | +# if __has_feature(objc_arc) |
| 258 | +# undef _lcl_logger_autoreleasepool_arc |
| 259 | +# define _lcl_logger_autoreleasepool_arc 1 |
| 260 | +# endif |
| 261 | +#endif |
| 262 | +#if _lcl_logger_autoreleasepool_arc |
| 263 | +# define _lcl_logger_autoreleasepool_begin \ |
| 264 | + @autoreleasepool { |
| 265 | +# define _lcl_logger_autoreleasepool_end \ |
| 266 | + } |
| 267 | +#else |
| 268 | +# define _lcl_logger_autoreleasepool_begin \ |
| 269 | + NSAutoreleasePool *_lcl_logger_autoreleasepool = [[NSAutoreleasePool alloc] init]; |
| 270 | +# define _lcl_logger_autoreleasepool_end \ |
| 271 | + [_lcl_logger_autoreleasepool release]; |
| 272 | +#endif |
| 273 | + |
| 274 | +#ifndef _LCL_NO_IGNORE_WARNINGS |
| 275 | +# ifdef __clang__ |
| 276 | + // Ignore some warnings about variadic macros when using '-Weverything'. |
| 277 | +# pragma clang diagnostic push |
| 278 | +# pragma clang diagnostic ignored "-Wunknown-pragmas" |
| 279 | +# pragma clang diagnostic ignored "-Wvariadic-macros" |
| 280 | +# pragma clang diagnostic ignored "-Wpedantic" |
| 281 | +# endif |
| 282 | +#endif |
| 283 | + |
| 284 | +// Define the _lcl_logger macro which integrates LCLLogFile as a logging |
| 285 | +// back-end for LibComponentLogging and pass the header of a log component as |
| 286 | +// the identifier to LCLLogFile's log method. |
| 287 | +#define _lcl_logger(_component, _level, _format, ...) { \ |
| 288 | + _lcl_logger_autoreleasepool_begin \ |
| 289 | + [LCLLogFile logWithIdentifier:_lcl_component_header[_component] \ |
| 290 | + level:_level \ |
| 291 | + path:__FILE__ \ |
| 292 | + line:__LINE__ \ |
| 293 | + function:__PRETTY_FUNCTION__ \ |
| 294 | + format:_format, \ |
| 295 | + ## __VA_ARGS__]; \ |
| 296 | + _lcl_logger_autoreleasepool_end \ |
| 297 | +} |
| 298 | + |
| 299 | +#ifndef _LCL_NO_IGNORE_WARNINGS |
| 300 | +# ifdef __clang__ |
| 301 | +# pragma clang diagnostic pop |
| 302 | +# endif |
| 303 | +#endif |
| 304 | + |
0 commit comments