Skip to content

Instantly share code, notes, and snippets.

@landonf
Created May 12, 2009 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save landonf/110767 to your computer and use it in GitHub Desktop.
Save landonf/110767 to your computer and use it in GitHub Desktop.
logging api example
/*
* Author: Landon Fuller <landonf@plausible.coop>
*
* Copyright (c) 2008 Plausible Labs.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of any contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
typedef enum {
PL_LOG_ERR,
PL_LOG_WARNING,
PL_LOG_INFO,
PL_LOG_DEBUG
} pl_loglevel_t;
@interface PLLog : NSObject {
}
#define DO_LOG_DECL(logName) \
+ (void) logName: (NSString *) message, ...;
DO_LOG_DECL(error);
DO_LOG_DECL(warning);
DO_LOG_DECL(info);
DO_LOG_DECL(debug);
#undef DO_LOG_DECL
+ (void) log: (pl_loglevel_t) level withMessage: (NSString *) message, ...;
@end
/*
* Author: Landon Fuller <landonf@plausible.coop>
*
* Copyright (c) 2008 Plausible Labs.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of any contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#import "PLLog.h"
#include <stdarg.h>
/**
* This logger provides programmer-oriented debug logging on an application-wide basis.
*/
@implementation PLLog
+ (void) log: (pl_loglevel_t) level withMessage: (NSString *) message withArgs: (va_list) args {
/* TODO: Filter on level */
NSLogv(message, args);
}
+ (void) log: (pl_loglevel_t) level withMessage: (NSString *) message, ... {
va_list ap;
/* Fetch the arguments */
va_start(ap, message);
[self log: level withMessage: message withArgs: ap];
va_end(ap);
}
/* Generate easy-to-use logging methods */
#define DO_LOG(logName, level) \
/** Log a priority message. */ \
+ (void) logName: (NSString *) message, ... { \
va_list ap; \
va_start(ap, message); \
[self log: level withMessage: message withArgs: ap]; \
va_end(ap); \
}
DO_LOG(error, PL_LOG_ERR);
DO_LOG(warning, PL_LOG_WARNING);
DO_LOG(info, PL_LOG_INFO);
DO_LOG(debug, PL_LOG_DEBUG);
#undef DO_LOG
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment