Skip to content

Commit

Permalink
Merge pull request CocoaLumberjack#1282 from jcbertin/revert-pr-1279
Browse files Browse the repository at this point in the history
Revert "Merge pull request CocoaLumberjack#1279 from kinarobin/optimization-date-for…
  • Loading branch information
sushichop authored Feb 9, 2022
2 parents 5fa8eef + 8472682 commit a7abce9
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 88 deletions.
83 changes: 2 additions & 81 deletions Sources/CocoaLumberjack/DDFileLogger.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@

NSTimeInterval const kDDRollingLeeway = 1.0; // 1s

unsigned int const kDDDefaultTimeStringBufferLength = 23;//string "0000/00/00 00:00:00:000"
unsigned long long const kDDDefaultSecForOneHour = 60 * 60;
unsigned long long const kDDDefaultSecForOneMinute = 60;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -539,9 +535,6 @@ - (NSString *)applicationName {

@interface DDLogFileFormatterDefault () {
NSDateFormatter *_dateFormatter;
time_t _timeZoneDeltaTime; // offset from UTC in seconds
__darwin_time_t _last_tv_sec;
char *_timeStringBuffer;
}

@end
Expand All @@ -562,88 +555,16 @@ - (instancetype)initWithDateFormatter:(nullable NSDateFormatter *)aDateFormatter
[_dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]];
[_dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[_dateFormatter setDateFormat:@"yyyy/MM/dd HH:mm:ss:SSS"];

char tempBuffer[] = "0000/00/00 00:00:00:000";
_timeStringBuffer = malloc(sizeof(char) * kDDDefaultTimeStringBufferLength);
memset(_timeStringBuffer, 0, strlen(tempBuffer));
memcpy(_timeStringBuffer, tempBuffer, strlen(tempBuffer));

time_t tm;
time(&tm);
struct tm *t_tm;
t_tm = localtime(&tm);
_timeZoneDeltaTime = t_tm->tm_gmtoff;
}
}

return self;
}

- (NSString *)formatLogMessage:(DDLogMessage *)logMessage {
/// Use default `dateFormatter`
if (_timeStringBuffer != NULL) {
const char *dateAndTime = [self formatLogTimestamp:logMessage->sys_timeval];
return [NSString stringWithFormat:@"%s %@", dateAndTime, logMessage->_message];
} else {
NSString *dateAndTime = [_dateFormatter stringFromDate:logMessage->_timestamp];
return [NSString stringWithFormat:@"%@ %@", dateAndTime, logMessage->_message];
}
}

- (const char *)formatLogTimestamp:(struct timeval)time {
__darwin_suseconds_t tv_usec = time.tv_usec;
__darwin_time_t tv_sec = time.tv_sec;

tv_usec = tv_usec + 500;
tv_sec = tv_sec + tv_usec / 1000000;
tv_usec = tv_usec % 1000000;

long currentDayCount = (tv_sec + _timeZoneDeltaTime) / (kDDDefaultSecForOneHour * 24);
long lastDayCount = (_last_tv_sec + _timeZoneDeltaTime) / (kDDDefaultSecForOneHour * 24);
if ((!_last_tv_sec) || (currentDayCount != lastDayCount)) {
// first log or new day
NSDate *date = [NSDate dateWithTimeIntervalSince1970:tv_sec];
NSString *dateString = [_dateFormatter stringFromDate:date];
if (dateString.length < 10) {
NSLogError(@"formatLogTimestamp dateString error: %@", dateString);
return NULL;
}
const char *dayCStr = [dateString substringToIndex:10].UTF8String;
memcpy(_timeStringBuffer, dayCStr, strlen(dayCStr));
}

long secOfDay = (tv_sec + _timeZoneDeltaTime) % (kDDDefaultSecForOneHour * 24);
long hour = secOfDay / kDDDefaultSecForOneHour;
long minute = secOfDay % kDDDefaultSecForOneHour / (kDDDefaultSecForOneHour * 24);
long second = secOfDay % kDDDefaultSecForOneMinute;

long milliSec = (tv_usec) / 1000;

#define DDLogFormatToBuffer(fieldValue, index, length) \
{ \
long tmp = fieldValue; \
long last = index; \
long len = length; \
while (len) { \
_timeStringBuffer[last] = '0' + (tmp % 10); \
last -= 1, tmp /= 10; \
len--; \
} \
}
DDLogFormatToBuffer(hour, 12, 2);
DDLogFormatToBuffer(minute, 15, 2);
DDLogFormatToBuffer(second, 18, 2);
DDLogFormatToBuffer(milliSec, 22, 3);

_last_tv_sec = tv_sec;
return _timeStringBuffer;
}
NSString *dateAndTime = [_dateFormatter stringFromDate:logMessage->_timestamp];

- (void)dealloc {
if (_timeStringBuffer != NULL) {
free(_timeStringBuffer);
_timeStringBuffer = NULL;
}
return [NSString stringWithFormat:@"%@ %@", dateAndTime, logMessage->_message];
}

@end
Expand Down
6 changes: 0 additions & 6 deletions Sources/CocoaLumberjack/DDLog.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#import <pthread.h>
#import <objc/runtime.h>
#import <sys/qos.h>
#import <sys/time.h>

#if TARGET_OS_IOS
#import <UIKit/UIDevice.h>
Expand Down Expand Up @@ -1009,11 +1008,6 @@ - (instancetype)initWithMessage:(NSString *)message
_options = options;
_timestamp = timestamp ?: [NSDate new];

struct timeval time;
gettimeofday(&time, NULL);
sys_timeval.tv_sec = time.tv_sec;
sys_timeval.tv_usec = time.tv_usec;

__uint64_t tid;
if (pthread_threadid_np(NULL, &tid) == 0) {
_threadID = [[NSString alloc] initWithFormat:@"%llu", tid];
Expand Down
1 change: 0 additions & 1 deletion Sources/CocoaLumberjack/include/CocoaLumberjack/DDLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,6 @@ typedef NS_OPTIONS(NSInteger, DDLogMessageOptions){
id _representedObject;
DDLogMessageOptions _options;
NSDate * _timestamp;
struct timeval sys_timeval;
NSString *_threadID;
NSString *_threadName;
NSString *_queueLabel;
Expand Down

0 comments on commit a7abce9

Please sign in to comment.