Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
mxq/mxq_log.c
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
63 lines (46 sloc)
1.02 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define _GNU_SOURCE | |
#include <stdio.h> | |
#include <errno.h> | |
#include <string.h> | |
#include <time.h> | |
#include <sys/types.h> | |
#include <unistd.h> | |
#include "mx_log.h" | |
#include "mx_util.h" | |
#ifndef mx_free_null | |
#include <stdlib.h> | |
#define mx_free_null(a) do { free((a)); (a) = NULL; } while(0) | |
#endif | |
static int timetag(char *buf, size_t size) | |
{ | |
time_t t; | |
struct tm *ltime; | |
size_t len; | |
if(!size) | |
return -(errno=EINVAL); | |
*buf = 0; | |
t = time(NULL); | |
if (t == ((time_t) -1)) | |
return -errno; | |
ltime = localtime(&t); | |
if (ltime == NULL) | |
return -(errno=EINVAL); | |
len = strftime(buf, size, "%F %T %z", ltime); | |
if (!len) | |
*buf = 0; | |
return len; | |
} | |
int mx_log_print(char *msg, size_t len) | |
{ | |
char timebuf[1024]; | |
if (!msg) { | |
return 0; | |
} | |
if (!len) | |
return 0; | |
timetag(timebuf, sizeof(timebuf)); | |
fprintf(stderr, "%s %s[%d]: %s\n", timebuf, program_invocation_short_name, getpid(), msg); | |
fflush(stderr); | |
mx_free_null(msg); | |
return 1; | |
} |