Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Copy file and function C string instance variables. #35

Closed
wants to merge 1 commit into from

2 participants

@tancred

The file and function instance variables aren't necessarily const,
e.g., when constructing a DDLogMessage by hand from some external
source.

Note: updated assignment/copy of the queueLabel ivar as well so
it uses the same code.

@tancred tancred Copy file and function C string instance variables.
The file and function instance variables aren't necessarily const,
e.g., when constructing a DDLogMessage by hand from some external
source.

Note: updated assignment/copy of the queueLabel ivar as well so
it uses the same code.
6139b66
@robbiehanson

Hmm... This possibility needs to be accounted for. But for most people, the variables are string literals, and so copying them doesn't make sense, and slows down the code.

@robbiehanson robbiehanson referenced this pull request from a commit
@robbiehanson robbiehanson Bug fix for issue #35 c7da108
@robbiehanson
enum {
    DDLogMessageCopyFile     = 1 << 0,
    DDLogMessageCopyFunction = 1 << 1,
};
typedef int DDLogMessageOptions;
/**
 * Standard init method for a log message object.
 * Used by the logging primitives. (And the macros use the logging primitives.)
 * 
 * If you find need to manually create logMessage objects, there is one thing you should be aware of:
 * 
 * If no flags are passed, the method expects the file and function parameters to be string literals.
 * That is, it expects the given strings to exist for the duration of the object's lifetime,
 * and it expects the given strings to be immutable.
 * In other words, it does not copy these strings, it simply points to them.
 * This is due to the fact that __FILE__ and __FUNCTION__ are usually used to specify these parameters,
 * so it makes sense to optimize and skip the unnecessary allocations.
 * However, if you need them to be copied you may use the options parameter to specify this.
 * Options is a bitmask which supports DDLogMessageCopyFile and DDLogMessageCopyFunction.
**/
- (id)initWithLogMsg:(NSString *)logMsg
               level:(int)logLevel
                flag:(int)logFlag
             context:(int)logContext
                file:(const char *)file
            function:(const char *)function
                line:(int)line
                 tag:(id)tag
             options:(DDLogMessageOptions)optionsMask;
@tancred

Nice fix. Thanks!

@ArloL ArloL referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 28, 2012
  1. @tancred

    Copy file and function C string instance variables.

    tancred authored
    The file and function instance variables aren't necessarily const,
    e.g., when constructing a DDLogMessage by hand from some external
    source.
    
    Note: updated assignment/copy of the queueLabel ivar as well so
    it uses the same code.
This page is out of date. Refresh to see the latest.
Showing with 21 additions and 11 deletions.
  1. +2 −2 Lumberjack/DDLog.h
  2. +19 −9 Lumberjack/DDLog.m
View
4 Lumberjack/DDLog.h
@@ -435,8 +435,8 @@ NSString *DDExtractFileNameWithoutExtension(const char *filePath, BOOL copy);
int logContext;
NSString *logMsg;
NSDate *timestamp;
- const char *file;
- const char *function;
+ char *file;
+ char *function;
int lineNumber;
mach_port_t machThreadID;
char *queueLabel;
View
28 Lumberjack/DDLog.m
@@ -793,6 +793,16 @@ - (void)dealloc
@implementation DDLogMessage
+static char *dd_c_string_copy(const char *aString)
+{
+ if (aString == NULL) return NULL;
+ size_t length = strlen(aString);
+ char * result = malloc(length + 1);
+ strncpy(result, aString, length);
+ result[length] = 0;
+ return result;
+}
+
- (id)initWithLogMsg:(NSString *)msg
level:(int)level
flag:(int)flag
@@ -808,8 +818,8 @@ - (id)initWithLogMsg:(NSString *)msg
logLevel = level;
logFlag = flag;
logContext = context;
- file = aFile;
- function = aFunction;
+ file = dd_c_string_copy(aFile);
+ function = dd_c_string_copy(aFunction);
lineNumber = line;
tag = aTag;
@@ -818,13 +828,7 @@ - (id)initWithLogMsg:(NSString *)msg
machThreadID = pthread_mach_thread_np(pthread_self());
const char *label = dispatch_queue_get_label(dispatch_get_current_queue());
- if (label)
- {
- size_t labelLength = strlen(label);
- queueLabel = malloc(labelLength+1);
- strncpy(queueLabel, label, labelLength);
- queueLabel[labelLength] = 0;
- }
+ queueLabel = dd_c_string_copy(label);
threadName = [[NSThread currentThread] name];
}
@@ -854,6 +858,12 @@ - (void)dealloc
if (queueLabel != NULL) {
free(queueLabel);
}
+ if (file != NULL) {
+ free(file);
+ }
+ if (function != NULL) {
+ free(function);
+ }
}
@end
Something went wrong with that request. Please try again.