-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWriteOutput.c
More file actions
59 lines (50 loc) · 1.65 KB
/
Copy pathWriteOutput.c
File metadata and controls
59 lines (50 loc) · 1.65 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
#include "WriteOutput.h"
pthread_mutex_t mutexWrite = PTHREAD_MUTEX_INITIALIZER;
struct timeval startTime;
void InitWriteOutput()
{
gettimeofday(&startTime, NULL);
}
unsigned long long GetTimestamp()
{
struct timeval currentTime;
gettimeofday(¤tTime, NULL);
return (currentTime.tv_sec - startTime.tv_sec) * 1000 // second
+ (currentTime.tv_usec - startTime.tv_usec) / 1000; // micro second
}
void PrintThreadId(FILE *f)
{
pthread_t tid = pthread_self();
size_t i;
fprintf(f, "ThreadID: ");
for (i=0; i<sizeof(pthread_t); ++i)
fprintf(f, "%02x", *(((unsigned char*)&tid) + i));
fprintf(f, ", ");
}
void WriteOutputf(FILE *f, int carID, char connector_type, int connectorID, Action action) {
unsigned long long time = GetTimestamp();
pthread_mutex_lock(&mutexWrite);
PrintThreadId(f);
fprintf(f,"CarID: %d, Object: %c%d, time stamp: %llu, AID: %d ", carID, connector_type, connectorID, time, (int)action);
switch (action) {
case TRAVEL:
fprintf(f, "traveling to connector.\n");
break;
case ARRIVE:
fprintf(f, "arrived at connector.\n");
break;
case START_PASSING:
fprintf(f, "started passing connector.\n");
break;
case FINISH_PASSING:
fprintf(f, "finished passing connector.\n");
break;
default:
fprintf(f, "Wrong argument format.\n");
break;
}
pthread_mutex_unlock(&mutexWrite);
}
void WriteOutput(int carID, char connector_type, int connectorID, Action action) {
WriteOutputf(stdout, carID, connector_type, connectorID, action);
}