source: trunk/packages/vizservers/nanovis/NvEventLog.cpp @ 1200

Last change on this file since 1200 was 1194, checked in by gah, 16 years ago
File size: 1.7 KB
Line 
1#include "config.h"
2#include <stdio.h>
3#include <assert.h>
4#include <sys/time.h>
5#include <sys/types.h>
6#include <unistd.h>
7#include <fcntl.h>
8#include <stdlib.h>
9#include <string.h>
10#include "NvEventLog.h"
11#include "nanovis.h"
12
13
14#ifdef XINETD
15FILE* xinetd_log;
16#endif
17
18FILE* event_log;
19double cur_time;        //in seconds
20
21#ifdef XINETD
22void NvInitService()
23{
24    const char* user = getenv("USER");
25    char* logName = NULL;
26    int logNameLen = 0;
27
28    if (user == NULL) {
29        logNameLen = 20+1;
30        logName = (char*) calloc(logNameLen,sizeof(char));
31        strncpy(logName,"/tmp/nanovis_log.txt",logNameLen);
32    }
33    else {
34        logNameLen = 17+1+strlen(user);
35        logName = (char*) calloc(logNameLen,sizeof(char));
36        strncpy(logName,"/tmp/nanovis_log_",logNameLen);
37        strncat(logName,user,strlen(user));
38    }
39
40    if (!NanoVis::debug_flag) {
41        //open log and map stderr to log file
42        xinetd_log = fopen(logName, "w");
43        close(2);
44        dup2(fileno(xinetd_log), 2);
45        dup2(2,1);
46        //flush junk
47        fflush(stdout);
48        fflush(stderr);
49    }
50
51    // clean up malloc'd memory
52    if (logName != NULL) {
53        free(logName);
54    }
55}
56
57void NvExitService()
58{
59    //close log file
60    if (xinetd_log != NULL) {
61        fclose(xinetd_log);
62        xinetd_log = NULL;
63    }
64}
65#endif
66
67void NvInitEventLog()
68{
69    event_log = fopen("event.txt", "w");
70    assert(event_log!=0);
71
72    struct timeval time;
73    gettimeofday(&time, NULL);
74    cur_time = time.tv_sec*1000. + time.tv_usec/1000.;
75}
76
77void NvExitEventLog()
78{
79    fclose(event_log);
80}
81
82double NvGetTimeInterval()
83{
84    struct timeval time;
85    gettimeofday(&time, NULL);
86    double new_time = time.tv_sec*1000. + time.tv_usec/1000.;
87
88    double interval = new_time - cur_time;
89    cur_time = new_time;
90    return interval;
91}
92
Note: See TracBrowser for help on using the repository browser.