3
3
4
4
import argparse
5
5
import base64
6
- import calendar
7
6
import errno
8
7
import gzip
9
8
import logging
16
15
import sys
17
16
import threading
18
17
import time
19
- from datetime import datetime , timedelta
18
+ from datetime import datetime
20
19
21
20
# from inspect import currentframe
22
21
# print(currentframe().f_lineno)
@@ -104,6 +103,7 @@ def __init__(
104
103
self .argv = argv
105
104
self .E : EnvParams = args .E
106
105
self .no_ansi = args .no_ansi
106
+ self .tz = UTC if args .log_utc else None
107
107
self .logf : Optional [typing .TextIO ] = None
108
108
self .logf_base_fn = ""
109
109
self .is_dut = False # running in unittest; always False
@@ -118,7 +118,8 @@ def __init__(
118
118
self .httpsrv_up = 0
119
119
120
120
self .log_mutex = threading .Lock ()
121
- self .next_day = 0
121
+ self .cday = 0
122
+ self .cmon = 0
122
123
self .tstack = 0.0
123
124
124
125
self .iphash = HMaccas (os .path .join (self .E .cfg , "iphash" ), 8 )
@@ -791,7 +792,7 @@ def _setlimits(self) -> None:
791
792
self .args .nc = min (self .args .nc , soft // 2 )
792
793
793
794
def _logname (self ) -> str :
794
- dt = datetime .now (UTC )
795
+ dt = datetime .now (self . tz )
795
796
fn = str (self .args .lo )
796
797
for fs in "YmdHMS" :
797
798
fs = "%" + fs
@@ -1064,12 +1065,12 @@ def _log_disabled(self, src: str, msg: str, c: Union[int, str] = 0) -> None:
1064
1065
return
1065
1066
1066
1067
with self .log_mutex :
1067
- zd = datetime .now (UTC )
1068
+ dt = datetime .now (self . tz )
1068
1069
ts = self .log_dfmt % (
1069
- zd .year ,
1070
- zd .month * 100 + zd .day ,
1071
- (zd .hour * 100 + zd .minute ) * 100 + zd .second ,
1072
- zd .microsecond // self .log_div ,
1070
+ dt .year ,
1071
+ dt .month * 100 + dt .day ,
1072
+ (dt .hour * 100 + dt .minute ) * 100 + dt .second ,
1073
+ dt .microsecond // self .log_div ,
1073
1074
)
1074
1075
1075
1076
if c and not self .args .no_ansi :
@@ -1090,41 +1091,26 @@ def _log_disabled(self, src: str, msg: str, c: Union[int, str] = 0) -> None:
1090
1091
if not self .args .no_logflush :
1091
1092
self .logf .flush ()
1092
1093
1093
- now = time .time ()
1094
- if int (now ) >= self .next_day :
1095
- self ._set_next_day ()
1094
+ if dt .day != self .cday or dt .month != self .cmon :
1095
+ self ._set_next_day (dt )
1096
1096
1097
- def _set_next_day (self ) -> None :
1098
- if self .next_day and self .logf and self .logf_base_fn != self ._logname ():
1097
+ def _set_next_day (self , dt : datetime ) -> None :
1098
+ if self .cday and self .logf and self .logf_base_fn != self ._logname ():
1099
1099
self .logf .close ()
1100
1100
self ._setup_logfile ("" )
1101
1101
1102
- dt = datetime .now (UTC )
1103
-
1104
- # unix timestamp of next 00:00:00 (leap-seconds safe)
1105
- day_now = dt .day
1106
- while dt .day == day_now :
1107
- dt += timedelta (hours = 12 )
1108
-
1109
- dt = dt .replace (hour = 0 , minute = 0 , second = 0 )
1110
- try :
1111
- tt = dt .utctimetuple ()
1112
- except :
1113
- # still makes me hella uncomfortable
1114
- tt = dt .timetuple ()
1115
-
1116
- self .next_day = calendar .timegm (tt )
1102
+ self .cday = dt .day
1103
+ self .cmon = dt .month
1117
1104
1118
1105
def _log_enabled (self , src : str , msg : str , c : Union [int , str ] = 0 ) -> None :
1119
1106
"""handles logging from all components"""
1120
1107
with self .log_mutex :
1121
- now = time .time ()
1122
- if int (now ) >= self .next_day :
1123
- dt = datetime .fromtimestamp (now , UTC )
1108
+ dt = datetime .now (self .tz )
1109
+ if dt .day != self .cday or dt .month != self .cmon :
1124
1110
zs = "{}\n " if self .no_ansi else "\033 [36m{}\033 [0m\n "
1125
1111
zs = zs .format (dt .strftime ("%Y-%m-%d" ))
1126
1112
print (zs , end = "" )
1127
- self ._set_next_day ()
1113
+ self ._set_next_day (dt )
1128
1114
if self .logf :
1129
1115
self .logf .write (zs )
1130
1116
@@ -1143,12 +1129,11 @@ def _log_enabled(self, src: str, msg: str, c: Union[int, str] = 0) -> None:
1143
1129
else :
1144
1130
msg = "%s%s\033 [0m" % (c , msg )
1145
1131
1146
- zd = datetime .fromtimestamp (now , UTC )
1147
1132
ts = self .log_efmt % (
1148
- zd .hour ,
1149
- zd .minute ,
1150
- zd .second ,
1151
- zd .microsecond // self .log_div ,
1133
+ dt .hour ,
1134
+ dt .minute ,
1135
+ dt .second ,
1136
+ dt .microsecond // self .log_div ,
1152
1137
)
1153
1138
msg = fmt % (ts , src , msg )
1154
1139
try :
0 commit comments