logcat: liblog: Add "usec" format argument

(cherry pick from commit e1f2004ecc)

- Add additional 3 digits of time precision for time output
  adding in the reporting of usec
- Remove trailing space in header file

Change-Id: Ifb560850b8e01080e126fbaeab640db71cce3eea
This commit is contained in:
Mark Salyzyn 2015-05-06 08:40:40 -07:00
parent a1aacb71f3
commit 79c3815ca1
3 changed files with 41 additions and 28 deletions

View File

@ -36,7 +36,9 @@ typedef enum {
FORMAT_TIME,
FORMAT_THREADTIME,
FORMAT_LONG,
FORMAT_COLOR,
/* The following two are modifiers to above formats */
FORMAT_MODIFIER_COLOR, /* converts priority to color */
FORMAT_MODIFIER_TIME_USEC, /* switches from msec to usec time precision */
} AndroidLogPrintFormat;
typedef struct AndroidLogFormat_t AndroidLogFormat;
@ -56,7 +58,8 @@ AndroidLogFormat *android_log_format_new();
void android_log_format_free(AndroidLogFormat *p_format);
void android_log_setPrintFormat(AndroidLogFormat *p_format,
/* currently returns 0 if format is a modifier, 1 if not */
int android_log_setPrintFormat(AndroidLogFormat *p_format,
AndroidLogPrintFormat format);
/**
@ -64,7 +67,7 @@ void android_log_setPrintFormat(AndroidLogFormat *p_format,
*/
AndroidLogPrintFormat android_log_formatFromString(const char *s);
/**
/**
* filterExpression: a single filter expression
* eg "AT:d"
*
@ -74,12 +77,12 @@ AndroidLogPrintFormat android_log_formatFromString(const char *s);
*
*/
int android_log_addFilterRule(AndroidLogFormat *p_format,
int android_log_addFilterRule(AndroidLogFormat *p_format,
const char *filterExpression);
/**
* filterString: a whitespace-separated set of filter expressions
/**
* filterString: a whitespace-separated set of filter expressions
* eg "AT:d *:i"
*
* returns 0 on success and -1 on invalid expression
@ -92,7 +95,7 @@ int android_log_addFilterString(AndroidLogFormat *p_format,
const char *filterString);
/**
/**
* returns 1 if this log line should be printed based on its priority
* and tag, and 0 if it should not
*/
@ -129,7 +132,7 @@ int android_log_processBinaryLogBuffer(struct logger_entry *buf,
* Returns NULL on malloc error
*/
char *android_log_formatLogLine (
char *android_log_formatLogLine (
AndroidLogFormat *p_format,
char *defaultBuffer,
size_t defaultBufferSize,

View File

@ -43,6 +43,7 @@ struct AndroidLogFormat_t {
FilterInfo *filters;
AndroidLogPrintFormat format;
bool colored_output;
bool usec_time_output;
};
/*
@ -185,6 +186,7 @@ AndroidLogFormat *android_log_format_new()
p_ret->global_pri = ANDROID_LOG_VERBOSE;
p_ret->format = FORMAT_BRIEF;
p_ret->colored_output = false;
p_ret->usec_time_output = false;
return p_ret;
}
@ -207,13 +209,19 @@ void android_log_format_free(AndroidLogFormat *p_format)
void android_log_setPrintFormat(AndroidLogFormat *p_format,
int android_log_setPrintFormat(AndroidLogFormat *p_format,
AndroidLogPrintFormat format)
{
if (format == FORMAT_COLOR)
if (format == FORMAT_MODIFIER_COLOR) {
p_format->colored_output = true;
else
p_format->format = format;
return 0;
}
if (format == FORMAT_MODIFIER_TIME_USEC) {
p_format->usec_time_output = true;
return 0;
}
p_format->format = format;
return 1;
}
/**
@ -231,7 +239,8 @@ AndroidLogPrintFormat android_log_formatFromString(const char * formatString)
else if (strcmp(formatString, "time") == 0) format = FORMAT_TIME;
else if (strcmp(formatString, "threadtime") == 0) format = FORMAT_THREADTIME;
else if (strcmp(formatString, "long") == 0) format = FORMAT_LONG;
else if (strcmp(formatString, "color") == 0) format = FORMAT_COLOR;
else if (strcmp(formatString, "color") == 0) format = FORMAT_MODIFIER_COLOR;
else if (strcmp(formatString, "usec") == 0) format = FORMAT_MODIFIER_TIME_USEC;
else format = FORMAT_OFF;
return format;
@ -745,7 +754,7 @@ char *android_log_formatLogLine (
struct tm tmBuf;
#endif
struct tm* ptm;
char timeBuf[32];
char timeBuf[32]; /* good margin, 23+nul for msec, 26+nul for usec */
char prefixBuf[128], suffixBuf[128];
char priChar;
int prefixSuffixIsHeaderFooter = 0;
@ -771,6 +780,14 @@ char *android_log_formatLogLine (
#endif
//strftime(timeBuf, sizeof(timeBuf), "%Y-%m-%d %H:%M:%S", ptm);
strftime(timeBuf, sizeof(timeBuf), "%m-%d %H:%M:%S", ptm);
len = strlen(timeBuf);
if (p_format->usec_time_output) {
snprintf(timeBuf + len, sizeof(timeBuf) - len,
".%06ld", entry->tv_nsec / 1000);
} else {
snprintf(timeBuf + len, sizeof(timeBuf) - len,
".%03ld", entry->tv_nsec / 1000000);
}
/*
* Construct a buffer containing the log header and log message.
@ -811,23 +828,21 @@ char *android_log_formatLogLine (
break;
case FORMAT_TIME:
len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
"%s.%03ld %c/%-8s(%5d): ", timeBuf, entry->tv_nsec / 1000000,
priChar, entry->tag, entry->pid);
"%s %c/%-8s(%5d): ", timeBuf, priChar, entry->tag, entry->pid);
strcpy(suffixBuf + suffixLen, "\n");
++suffixLen;
break;
case FORMAT_THREADTIME:
len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
"%s.%03ld %5d %5d %c %-8s: ", timeBuf, entry->tv_nsec / 1000000,
"%s %5d %5d %c %-8s: ", timeBuf,
entry->pid, entry->tid, priChar, entry->tag);
strcpy(suffixBuf + suffixLen, "\n");
++suffixLen;
break;
case FORMAT_LONG:
len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
"[ %s.%03ld %5d:%5d %c/%-8s ]\n",
timeBuf, entry->tv_nsec / 1000000, entry->pid,
entry->tid, priChar, entry->tag);
"[ %s %5d:%5d %c/%-8s ]\n",
timeBuf, entry->pid, entry->tid, priChar, entry->tag);
strcpy(suffixBuf + suffixLen, "\n\n");
suffixLen += 2;
prefixSuffixIsHeaderFooter = 1;

View File

@ -235,7 +235,7 @@ static void show_help(const char *cmd)
" -r <kbytes> Rotate log every kbytes. Requires -f\n"
" -n <count> Sets max number of rotated logs to <count>, default 4\n"
" -v <format> Sets the log print format, where <format> is:\n\n"
" brief color long process raw tag thread threadtime time\n\n"
" brief color long process raw tag thread threadtime time usec\n\n"
" -D print dividers between each log buffer\n"
" -c clear (flush) the entire log and exit\n"
" -d dump the log and then exit (don't block)\n"
@ -291,9 +291,7 @@ static int setLogFormat(const char * formatString)
return -1;
}
android_log_setPrintFormat(g_logformat, format);
return 0;
return android_log_setPrintFormat(g_logformat, format);
}
static const char multipliers[][2] = {
@ -569,10 +567,7 @@ int main(int argc, char **argv)
if (err < 0) {
logcat_panic(true, "Invalid parameter %s to -v\n", optarg);
}
if (strcmp("color", optarg)) { // exception for modifiers
hasSetLogFormat = 1;
}
hasSetLogFormat |= err;
break;
case 'Q':