forked from edman007/chiton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.cpp
134 lines (119 loc) · 3.64 KB
/
util.cpp
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**************************************************************************
*
* This file is part of Chiton.
*
* Chiton is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Chiton is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Chiton. If not, see <https://www.gnu.org/licenses/>.
*
* Copyright 2020 Ed Martin <[email protected]>
*
**************************************************************************
*/
#include "util.hpp"
#include <sys/time.h>
#include <syslog.h>
std::mutex Util::lock;
#ifdef DEBUG
unsigned int Util::log_level = 5;
#else
unsigned int Util::log_level = 3;
#endif
bool Util::use_syslog = false;
void Util::log_msg(const LOG_LEVEL lvl, const std::string& msg){
if (lvl > log_level){
return;//drop any message above our current logging level
}
if (use_syslog){
int priority = LOG_DEBUG;
switch (lvl){
case CH_LOG_FATAL:
priority = LOG_CRIT;
break;
case CH_LOG_ERROR:
priority = LOG_ERR;
break;
case CH_LOG_WARN:
priority = LOG_WARNING;
break;
case CH_LOG_INFO:
priority = LOG_INFO;
break;
case CH_LOG_DEBUG:
priority = LOG_DEBUG;
break;
}
syslog(priority, "%s", msg.c_str());
} else {
if (lvl == CH_LOG_ERROR || lvl == CH_LOG_FATAL){
lock.lock();
std::cerr << msg << std::endl;
lock.unlock();
} else {
lock.lock();
std::cout << msg << std::endl;
lock.unlock();
}
}
}
void Util::get_videotime(struct timeval &time){
gettimeofday(&time, NULL);
}
void Util::get_time_parts(const struct timeval &time, struct VideoDate &date){
struct tm out;
date.ms = time.tv_usec / 1000;
localtime_r(&time.tv_sec, &out);
//copy it into our format
date.year = out.tm_year + 1900;
date.month = out.tm_mon + 1;
date.day = out.tm_mday;
date.hour = out.tm_hour;
date.min = out.tm_min;
date.sec = out.tm_sec;
}
unsigned long long int Util::pack_time(const struct timeval &time){
unsigned long long int out;
out = time.tv_sec;
out *= 1000;
out += time.tv_usec / 1000;
return out;
}
void Util::unpack_time(const unsigned long long int packed_time, struct timeval &time){
time.tv_usec = (packed_time % 1000) * 1000;
time.tv_sec = packed_time / 1000;
}
void Util::compute_timestamp(const struct timeval &connect_time, struct timeval &out_time, long pts, AVRational &time_base){
double delta = av_q2d(time_base) * pts;
double usec = delta - ((long)delta);
usec *= 1000000;
out_time.tv_sec = connect_time.tv_sec + delta;
out_time.tv_usec = connect_time.tv_usec + usec;
if (out_time.tv_usec >= 1000000){
out_time.tv_usec -= 1000000;
out_time.tv_sec++;
}
}
void Util::set_log_level(unsigned int level){
log_level = level;
}
bool Util::enable_syslog(void){
openlog("chiton", LOG_PID, LOG_USER);
use_syslog = true;
return use_syslog;
}
bool Util::disable_syslog(void){
if (use_syslog){
closelog();
use_syslog = false;
}
return !use_syslog;
}