-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtlog.cpp
More file actions
85 lines (74 loc) · 1.96 KB
/
tlog.cpp
File metadata and controls
85 lines (74 loc) · 1.96 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
/*
per-connection MAVProxy-format tlog writer
*/
#include "tlog.h"
#include "session.h"
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
TlogWriter::~TlogWriter()
{
close();
}
bool TlogWriter::open(uint32_t port2, unsigned session_n, const char *base_dir)
{
if (fp != nullptr) {
return true;
}
time_t now = time(nullptr);
struct tm tm_now;
localtime_r(&now, &tm_now);
char dir[768];
snprintf(dir, sizeof(dir), "%s/%u/%04d-%02d-%02d",
base_dir, port2,
tm_now.tm_year + 1900,
tm_now.tm_mon + 1,
tm_now.tm_mday);
if (mkpath_0700(dir) < 0) {
::printf("tlog: mkdir %s failed: %s\n", dir, strerror(errno));
return false;
}
char path[1024];
snprintf(path, sizeof(path), "%s/session%u.tlog", dir, session_n);
fp = fopen(path, "ab");
if (fp == nullptr) {
::printf("tlog: fopen %s failed: %s\n", path, strerror(errno));
return false;
}
// Unbuffered: each frame goes straight to write() so the file is
// readable in real-time (e.g. during a live download) and survives
// a child crash without losing the stdio buffer.
setvbuf(fp, nullptr, _IONBF, 0);
::printf("tlog: %s\n", path);
return true;
}
void TlogWriter::write_frame(const uint8_t *frame, size_t len)
{
if (fp == nullptr || frame == nullptr || len == 0) {
return;
}
struct timeval tv;
gettimeofday(&tv, nullptr);
uint64_t us = uint64_t(tv.tv_sec) * 1000000ULL + uint64_t(tv.tv_usec);
uint8_t hdr[8];
for (int i = 0; i < 8; i++) {
hdr[i] = uint8_t((us >> ((7 - i) * 8)) & 0xff);
}
fwrite(hdr, 1, 8, fp);
fwrite(frame, 1, len, fp);
}
void TlogWriter::close()
{
if (fp != nullptr) {
fflush(fp);
fsync(fileno(fp));
fclose(fp);
fp = nullptr;
}
}