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

Last change on this file since 1049 was 599, checked in by dkearney, 17 years ago

created dynamic log file names to resolve the problem with different users accessing the same logfile.

File size: 1.6 KB
Line 
1#include "NvEventLog.h"
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
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    //open log and map stderr to log file
41    xinetd_log = fopen(logName, "w");
42    close(2);
43    dup2(fileno(xinetd_log), 2);
44    dup2(2,1);
45
46    //flush junk
47    fflush(stdout);
48    fflush(stderr);
49
50    // clean up malloc'd memory
51    if (logName != NULL) {
52        free(logName);
53    }
54}
55
56void NvExitService()
57{
58    //close log file
59    fclose(xinetd_log);
60}
61#endif
62
63void NvInitEventLog()
64{
65    event_log = fopen("event.txt", "w");
66    assert(event_log!=0);
67
68    struct timeval time;
69    gettimeofday(&time, NULL);
70    cur_time = time.tv_sec*1000. + time.tv_usec/1000.;
71}
72
73void NvExitEventLog()
74{
75    fclose(event_log);
76}
77
78double NvGetTimeInterval()
79{
80    struct timeval time;
81    gettimeofday(&time, NULL);
82    double new_time = time.tv_sec*1000. + time.tv_usec/1000.;
83
84    double interval = new_time - cur_time;
85    cur_time = new_time;
86    return interval;
87}
88
Note: See TracBrowser for help on using the repository browser.