1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
2 | #include "config.h" |
---|
3 | #include <stdio.h> |
---|
4 | #include <assert.h> |
---|
5 | #include <sys/time.h> |
---|
6 | #include <sys/types.h> |
---|
7 | #include <unistd.h> |
---|
8 | #include <fcntl.h> |
---|
9 | #include <stdlib.h> |
---|
10 | #include <string.h> |
---|
11 | #include "NvEventLog.h" |
---|
12 | #include "nanovis.h" |
---|
13 | |
---|
14 | |
---|
15 | FILE* event_log; |
---|
16 | double cur_time; //in seconds |
---|
17 | |
---|
18 | #ifdef XINETD |
---|
19 | void NvInitService() |
---|
20 | { |
---|
21 | const char* user = getenv("USER"); |
---|
22 | char* logName = NULL; |
---|
23 | int logNameLen = 0; |
---|
24 | |
---|
25 | if (user == NULL) { |
---|
26 | logNameLen = 20+1; |
---|
27 | logName = (char*) calloc(logNameLen,sizeof(char)); |
---|
28 | strncpy(logName,"/tmp/nanovis_log.txt",logNameLen); |
---|
29 | } else { |
---|
30 | logNameLen = 17+1+strlen(user); |
---|
31 | logName = (char*) calloc(logNameLen,sizeof(char)); |
---|
32 | strncpy(logName,"/tmp/nanovis_log_",logNameLen); |
---|
33 | strncat(logName, user, strlen(user)); |
---|
34 | } |
---|
35 | |
---|
36 | //open log and map stderr to log file |
---|
37 | NanoVis::logfile = fopen(logName, "w"); |
---|
38 | dup2(fileno(NanoVis::logfile), 2); |
---|
39 | /* dup2(2,1); */ |
---|
40 | |
---|
41 | // clean up malloc'd memory |
---|
42 | if (logName != NULL) { |
---|
43 | free(logName); |
---|
44 | } |
---|
45 | } |
---|
46 | |
---|
47 | void NvExitService() |
---|
48 | { |
---|
49 | //close log file |
---|
50 | if (NanoVis::logfile != NULL) { |
---|
51 | fclose(NanoVis::logfile); |
---|
52 | NanoVis::logfile = NULL; |
---|
53 | } |
---|
54 | } |
---|
55 | #endif |
---|
56 | |
---|
57 | void NvInitEventLog() |
---|
58 | { |
---|
59 | event_log = fopen("event.txt", "w"); |
---|
60 | assert(event_log!=0); |
---|
61 | |
---|
62 | struct timeval time; |
---|
63 | gettimeofday(&time, NULL); |
---|
64 | cur_time = time.tv_sec*1000. + time.tv_usec/1000.; |
---|
65 | } |
---|
66 | |
---|
67 | void NvExitEventLog() |
---|
68 | { |
---|
69 | fclose(event_log); |
---|
70 | } |
---|
71 | |
---|
72 | double NvGetTimeInterval() |
---|
73 | { |
---|
74 | struct timeval time; |
---|
75 | |
---|
76 | gettimeofday(&time, NULL); |
---|
77 | double new_time = time.tv_sec*1000. + time.tv_usec/1000.; |
---|
78 | |
---|
79 | double interval = new_time - cur_time; |
---|
80 | cur_time = new_time; |
---|
81 | return interval; |
---|
82 | } |
---|
83 | |
---|