-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.c
More file actions
110 lines (84 loc) · 1.87 KB
/
lib.c
File metadata and controls
110 lines (84 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* This file is part of uxlaunch
*
* (C) Copyright 2009 Intel Corporation
* Authors:
* Auke Kok <auke@linux.intel.com>
* Arjan van de Ven <arjan@linux.intel.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; version 2
* of the License.
*/
#include <unistd.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/time.h>
#include <sys/types.h>
#include <string.h>
#include "uxlaunch.h"
extern char **environ;
static int first_time = 1;
static int logfile_enabled = 1;
struct timeval start;
static FILE *log;
void open_log(const char *logfile)
{
/* truncate log */
if (!logfile)
log = stdout;
else
log = fopen(logfile, "w");
if (!log)
logfile_enabled = 0;
}
void lprintf(const char* fmt, ...)
{
va_list args;
struct timeval current;
uint64_t secs, usecs;
char string[8192];
char msg[8192];
if (first_time) {
first_time = 0;
gettimeofday(&start, NULL);
}
gettimeofday(¤t, NULL);
secs = current.tv_sec - start.tv_sec;
while (current.tv_usec < start.tv_usec) {
secs --;
current.tv_usec += 1000000;
}
usecs = current.tv_usec - start.tv_usec;
va_start(args, fmt);
vsnprintf(msg, 8192, fmt, args);
va_end(args);
if (msg[strlen(msg) - 1] == '\n')
msg[strlen(msg) - 1] = '\0';
snprintf(string, 8192, "[%02llu.%06llu] [%d] %s\n", secs, usecs, getpid(), msg);
if (verbose)
fprintf(stderr, "%s", string);
if (!logfile_enabled)
return;
if (log) {
fputs(string, log);
fflush(log);
} else {
logfile_enabled = 0;
lprintf("unable to write logfile, file logging disabled");
}
}
void log_environment(void)
{
char **env;
env = environ;
lprintf("--- environment variable dump:");
while (*env) {
lprintf(" %s", *env);
env++;
}
lprintf("---");
}