Blame view

src/core/pyros_django/agent/logpyros.py 12.3 KB
4ba41afb   Alain Klotz   Debut d'integrati...
1
2
3
import os
import math
import tempfile
1bb025d7   Alain Klotz   Mise en oeuvre de...
4
5
from io import StringIO
import sys
4ba41afb   Alain Klotz   Debut d'integrati...
6

e0bf1dda   Alain Klotz   Correction de mod...
7
# --- update the path of Python in case to test this file alone
4ba41afb   Alain Klotz   Debut d'integrati...
8
py_path = os.sys.path
e0bf1dda   Alain Klotz   Correction de mod...
9
py_pwd = os.path.normpath(os.getcwd() + "/../../../..")
4ba41afb   Alain Klotz   Debut d'integrati...
10
11
12
if (py_pwd not in py_path):
    (os.sys.path).append(py_pwd)

4f2a9fef   Alain Klotz   Ameliorations des...
13
# --- import PyROS celestial mechanics from the pyros root directory
c6c8be74   Etienne Pallier   Restructuration d...
14
import src.core.celme as celme
4ba41afb   Alain Klotz   Debut d'integrati...
15

4ba41afb   Alain Klotz   Debut d'integrati...
16
class LogPyros:
4f2a9fef   Alain Klotz   Ameliorations des...
17
    """
4f2a9fef   Alain Klotz   Ameliorations des...
18
    Manage PyROS logs in display, file and database.
15bc2c12   Etienne Pallier   Check fin executi...
19

4f2a9fef   Alain Klotz   Ameliorations des...
20
21
22
23
24
    First, create an instance:
    log = LogPyros("test",None)

    Second, use the print methods to log:
    log.print("Something to log")
4f2a9fef   Alain Klotz   Ameliorations des...
25
    """
4ba41afb   Alain Klotz   Debut d'integrati...
26

1bb025d7   Alain Klotz   Mise en oeuvre de...
27
28
29
30
31
32
33
    # === Level of log
    LOG_LEVEL_INFO = 0
    LOG_LEVEL_WARNING = -1
    LOG_LEVEL_ERROR = -2
    LOG_LEVEL_DEBUG = 1
    LOG_LEVEL_DEBUG2 = 2
    
4ba41afb   Alain Klotz   Debut d'integrati...
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
    # === Constants
    NO_ERROR = 0

    ERR_ALIAS_NAME_NOT_DEFINED = 10
    
    ERR_FILE_NOT_EXISTS = 101
    ERR_PATH_NOT_EXISTS = 102
    ERR_EMPTY_FILENAME = 103
    ERR_EMPTY_PATHNAME = 104

    # === Private variables
    _last_errno = NO_ERROR
        
    _path_data = ''
    _agent_alias = ''
    _path_data_log  = ''
    _path_wwwpyros = ''
    
    _date = None
    _home = None
    _noon_hour = 12
4f2a9fef   Alain Klotz   Ameliorations des...
55
56
57
    _last_lines = list()
    _nbmax_last_lines = 30

4ba41afb   Alain Klotz   Debut d'integrati...
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
# =====================================================================
# =====================================================================
# Private methods
# =====================================================================
# =====================================================================

    def _mkdir_path_log(self):
        # ---
        if self._agent_alias == "":
            self._last_errno = self.ERR_EMPTY_FILENAME
        else:
            self._path_www_log = self._path_wwwpyros + "/logs/" + self._agent_alias
            os.mkdir(self._path_www_log)
        # ---
        if self._path_data == "":
            self._last_errno = self.ERR_EMPTY_PATHNAME
        else:
            self._path_data_log = self._path_data + "/logs/" + self._agent_alias
            os.mkdir(self._path_data_log)
        
    def _jd2datedigit(self, jd):
        """ Convert a julian day into a string of YYYYMMDD
        """
        datetmp = self._date.date(jd);
        datedigit = (datetmp.digits(0))[0:8]
        return datedigit

    def _date2night(self, date):
        # night is the truncated part of the date of the previous noon 
        self._date.date(date)
        jd = self._date.jd()
        jd0 = math.floor(jd) - self._noon_hour/24
        d = celme.Date(jd0)
        djd = jd-jd0
        # print(f"jd0 = {d.iso()} djd={djd}")
        if djd>=1:
            jd0 += 1
        djd = jd-jd0
        if djd>=1:
            jd0 += 1
        d = celme.Date(jd0)
        night = d.digits(0)[0:8]
        return night

    def _night2dates(self, night):
        """
        jd_noon0 = previous noon
        jd_midnight = midnight instant
        jd_noon1 = next noon
        """
        d = celme.Date(night)
        d_noon0 = d + self._noon_hour/24
        jd_noon0 = d_noon0.jd()
        jd_midnight = jd_noon0 + 0.5
        jd_noon1 = jd_noon0 + 1.0
        return jd_noon0, jd_midnight, jd_noon1
    
1bb025d7   Alain Klotz   Mise en oeuvre de...
115
116
117
118
119
120
121
122
123
124
125
    def _convert_args2str(self, *args, **kwargs):
        """
        return a string as the result of a print method
        """
        bak = sys.stdout # on sauvegarde l'ancien stdout
        result = StringIO()
        sys.stdout = result
        print (*args, **kwargs,end='')
        sys.stdout = bak # on restore stdout
        return result.getvalue()

4ba41afb   Alain Klotz   Debut d'integrati...
126
127
# =====================================================================
# =====================================================================
1bb025d7   Alain Klotz   Mise en oeuvre de...
128
# Private methods getter/setter
4ba41afb   Alain Klotz   Debut d'integrati...
129
130
131
# =====================================================================
# =====================================================================

1bb025d7   Alain Klotz   Mise en oeuvre de...
132
133
134
135
136
137
138
139
140
141
142
143
    def _set_debug_level(self, level:str):
        if type(level)=="bool":
            if level==True:
                level = 1
            else:
                level = 0
        self._debug_level = level
        
    def _get_debug_level(self):
        return self._debug_level
        
    def _set_agent_alias(self, agent_alias:str):
4ba41afb   Alain Klotz   Debut d'integrati...
144
145
146
147
148
149
        if agent_alias=="":
            self._last_errno = self.ERR_ALIAS_NAME_NOT_DEFINED
            raise Exception(f"Agent alias must be not be empty string.")
        self._agent_alias = agent_alias
        return self.NO_ERROR

1bb025d7   Alain Klotz   Mise en oeuvre de...
150
    def _get_agent_alias(self):
4ba41afb   Alain Klotz   Debut d'integrati...
151
152
        return self._agent_alias

1bb025d7   Alain Klotz   Mise en oeuvre de...
153
    def _set_path_data(self, path_data:str):
4ba41afb   Alain Klotz   Debut d'integrati...
154
155
156
157
158
159
        if not os.path.exists(path_data):
            self._last_errno = self.ERR_PATH_NOT_EXISTS
            raise Exception(f"Path '{path_data}' for data does not exists. Create it first manually.")
        self._path_data = path_data
        return self.NO_ERROR

1bb025d7   Alain Klotz   Mise en oeuvre de...
160
    def _get_path_data(self):
4ba41afb   Alain Klotz   Debut d'integrati...
161
162
        return self._path_data

1bb025d7   Alain Klotz   Mise en oeuvre de...
163
    def _set_path_wwwpyros(self, path_wwwpyros:str):
4ba41afb   Alain Klotz   Debut d'integrati...
164
165
166
167
168
169
        if not os.path.exists(path_wwwpyros):
            self._path_wwwpyros = self.ERR_PATH_NOT_EXISTS
            raise Exception(f"Path '{path_wwwpyros}' for wwwpyros does not exists. Create it first manually.")
        self._path_wwwpyros = path_wwwpyros
        return self.NO_ERROR

1bb025d7   Alain Klotz   Mise en oeuvre de...
170
    def _get_path_wwwpyros(self):
4ba41afb   Alain Klotz   Debut d'integrati...
171
172
        return self._path_wwwpyros

1bb025d7   Alain Klotz   Mise en oeuvre de...
173
    def _set_home(self, home:str):
4ba41afb   Alain Klotz   Debut d'integrati...
174
175
176
177
178
179
        self._home.home(home)
        longitude = self._home.longitude
        self._noon_hour = (180 - longitude)/15.0
        # - place noon_hour in the range [0 24[
        if self._noon_hour < 0:
            self._noon_hour += 24
1bb025d7   Alain Klotz   Mise en oeuvre de...
180
        return self.NO_ERROR
4ba41afb   Alain Klotz   Debut d'integrati...
181

1bb025d7   Alain Klotz   Mise en oeuvre de...
182
    def _get_home(self):
4ba41afb   Alain Klotz   Debut d'integrati...
183
        return self._home.gps
1bb025d7   Alain Klotz   Mise en oeuvre de...
184
    
a760bcc8   Alain Klotz   Mise à jour de Lo...
185
186
187
188
189
190
    def _set_logtable(self, logtable):
        self._logtable = logtable
        return self.NO_ERROR

    def _get_logtable(self):
        return self._logtable        
4f2a9fef   Alain Klotz   Ameliorations des...
191
192
193
194
195
196
197

    def _set_nbmax_last_lines(self, nbmax_last_lines):
        self._nbmax_last_lines = nbmax_last_lines
        return self.NO_ERROR

    def _get_nbmax_last_lines(self):
        return self._nbmax_last_lines        
a760bcc8   Alain Klotz   Mise à jour de Lo...
198
    
1bb025d7   Alain Klotz   Mise en oeuvre de...
199
200
201
202
203
# =====================================================================
# =====================================================================
# Property methods
# =====================================================================
# =====================================================================
5b21ebad   Etienne Pallier   Utilisation de pa...
204

1bb025d7   Alain Klotz   Mise en oeuvre de...
205
206
207
208
209
210
211
    agent_alias = property(_get_agent_alias, _set_agent_alias)
    
    path_data = property(_get_path_data, _set_path_data)
    
    path_wwwpyros = property(_get_path_wwwpyros, _set_path_wwwpyros)
    
    home = property(_get_home, _set_home)
a760bcc8   Alain Klotz   Mise à jour de Lo...
212
213

    logtable = property(_get_logtable, _set_logtable)
4f2a9fef   Alain Klotz   Ameliorations des...
214
215
216
217
218
219
220
221
222
223
    
# =====================================================================
# =====================================================================
# Public Property methods
# =====================================================================
# =====================================================================

    debug_level = property(_get_debug_level, _set_debug_level)
    
    nbmax_last_lines = property(_get_nbmax_last_lines, _set_nbmax_last_lines)
1bb025d7   Alain Klotz   Mise en oeuvre de...
224
225
226
227
228
229
230
231
        
# =====================================================================
# =====================================================================
# Methods for users
# =====================================================================
# =====================================================================
        
    def print(self, *args, **kwargs):
4f2a9fef   Alain Klotz   Ameliorations des...
232
233
234
235
236
237
238
        """
        This is the method to print in the console display and in a log file.
        In the console display, the message is presented as:
            (Agent_name) message
        In the log file the message is presented as:
            Date-ISO message
        """
1bb025d7   Alain Klotz   Mise en oeuvre de...
239
240
241
242
243
244
        msg = self._convert_args2str(*args, **kwargs)
        # --- prepare the super_msg
        if msg=="":
            super_msg = msg
        else:
            super_msg = f"({self._agent_alias}) {msg}"
4ba41afb   Alain Klotz   Debut d'integrati...
245
        # --- classical print
1bb025d7   Alain Klotz   Mise en oeuvre de...
246
        print(super_msg)
4ba41afb   Alain Klotz   Debut d'integrati...
247
248
249
250
251
        # --- no more if the agent name is not defined
        if self._agent_alias=="":
            return
        # --- 
        self.file(msg)
5b21ebad   Etienne Pallier   Utilisation de pa...
252

1bb025d7   Alain Klotz   Mise en oeuvre de...
253
    def printd(self, *args, **kwargs):
4f2a9fef   Alain Klotz   Ameliorations des...
254
255
256
        """
        Same as print method but only if debug level is > threashold
        """
1bb025d7   Alain Klotz   Mise en oeuvre de...
257
258
259
260
        if self.debug_level > 0:
            self.print(*args, **kwargs)
            
    def file(self, *args, **kwargs):
4f2a9fef   Alain Klotz   Ameliorations des...
261
262
263
264
265
266
        """
        This is the method to print in a log file.
        In the log file the message is presented as:
            Date-ISO message
        The last file is also apended with the message
        """
1bb025d7   Alain Klotz   Mise en oeuvre de...
267
        msg = self._convert_args2str(*args, **kwargs)
4ba41afb   Alain Klotz   Debut d'integrati...
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
        # --- no more if the agent name is not defined
        if self._agent_alias=="":
            return
        # --- check the path to write the log file
        path = os.path.normpath(self._path_data + os.sep + "logs" + os.sep + self._agent_alias)
        if not os.path.exists(path):
            try:
                os.makedirs(path)
            except:            
                p = f"Cannot create path {path}"
                raise Exception(p)
        # --- compute the current night digital date
        night = self._date2night("now")
        # --- build the log file name
        file = os.path.normpath(path + os.sep + self._agent_alias + '_' + night + '.log')
        # --- prepare the super_msg
        self._date.date("NOW")
        super_msg = f"{self._date.iso()} {msg}"
        # --- write super_msg in the log file
        with open(file,'a') as fic:
            fic.write(super_msg+"\n")
4f2a9fef   Alain Klotz   Ameliorations des...
289
290
291
292
293
294
295
296
297
298
299
300
        # --- last lines
        self._last_lines.append(super_msg)
        n = len(self._last_lines)
        if n>self.nbmax_last_lines:
            self._last_lines = self._last_lines[n-self.nbmax_last_lines:]
        # --- build the log file name
        file = os.path.normpath(path + os.sep + self._agent_alias + '_' + 'last' + '.log')
        # --- write super_msg in the log file
        with open(file,'w') as fic:
            for line in self._last_lines:
                fic.write(line+"\n")
        
5b21ebad   Etienne Pallier   Utilisation de pa...
301

1bb025d7   Alain Klotz   Mise en oeuvre de...
302
    def db(self, *args, **kwargs):
4f2a9fef   Alain Klotz   Ameliorations des...
303
304
305
306
307
        """
        This is the method to print in the agent_log table of the database.
        """
        if self.logtable==None:
            return
1bb025d7   Alain Klotz   Mise en oeuvre de...
308
        msg = self._convert_args2str(*args, **kwargs)
4f2a9fef   Alain Klotz   Ameliorations des...
309
        self.logtable.objects.create(name=self._agent_alias, message=msg)
4ba41afb   Alain Klotz   Debut d'integrati...
310
311
312
313
314
315
316
    
# =====================================================================
# =====================================================================
# Special methods
# =====================================================================
# =====================================================================
        
4f2a9fef   Alain Klotz   Ameliorations des...
317
    def __init__(self, agent_alias:str, logtable=None, home:str="GPS 0 E 43 150", path_data:str="", path_wwwpyros:str=""  ):
4ba41afb   Alain Klotz   Debut d'integrati...
318
319
320
        self._last_errno = self.NO_ERROR
        # ---
        if agent_alias != "":
1bb025d7   Alain Klotz   Mise en oeuvre de...
321
            self.agent_alias = agent_alias
4f2a9fef   Alain Klotz   Ameliorations des...
322
323
        else:
            self.agent_alias = "Unknown_agent"
4ba41afb   Alain Klotz   Debut d'integrati...
324
325
        # ---
        if path_data != "":
1bb025d7   Alain Klotz   Mise en oeuvre de...
326
            self.path_data = path_data
4ba41afb   Alain Klotz   Debut d'integrati...
327
        else:
1bb025d7   Alain Klotz   Mise en oeuvre de...
328
            self.path_data = tempfile.gettempdir()
4ba41afb   Alain Klotz   Debut d'integrati...
329
330
        # ---
        if path_wwwpyros != "":
1bb025d7   Alain Klotz   Mise en oeuvre de...
331
            self.path_wwwpyros = path_wwwpyros
4ba41afb   Alain Klotz   Debut d'integrati...
332
        self._date = celme.Date()
4f2a9fef   Alain Klotz   Ameliorations des...
333
334
        self._home = celme.Home(home)
        self._noon_hour = 12 ; # local hour corresponding to the date change
a760bcc8   Alain Klotz   Mise à jour de Lo...
335
        self.logtable = None
4f2a9fef   Alain Klotz   Ameliorations des...
336
        self.nbmax_last_lines = 30
4ba41afb   Alain Klotz   Debut d'integrati...
337
338
339
340
341
342
343
344

# =====================================================================
# =====================================================================
# Test if main
# =====================================================================
# =====================================================================
    
if __name__ == "__main__":
4f2a9fef   Alain Klotz   Ameliorations des...
345
346
347
348
349
350
    
    # === Instance parameters
    
    # --- agent_alias = string added at the begining of each line of display log 
    # --- agent_alias = is the name of log files
    # --- agent_alias = is the field name of database logs
4ba41afb   Alain Klotz   Debut d'integrati...
351
    agent_alias = "test"
4f2a9fef   Alain Klotz   Ameliorations des...
352
    # --- agent_alias = directory where are written log files 
4ba41afb   Alain Klotz   Debut d'integrati...
353
    path_data = "c:/srv/work/pyros"
4f2a9fef   Alain Klotz   Ameliorations des...
354
355
    # --- home = define the hour of noon when log files are archived 
    home = "GPS 2 E 43 148"
4ba41afb   Alain Klotz   Debut d'integrati...
356
    
4f2a9fef   Alain Klotz   Ameliorations des...
357
358
359
360
361
362
363
    # === Instanciation

    # --- The second parameter is the Django object for database   
    log = LogPyros(agent_alias,None,home,path_data)
    
    # === Use of logs. Parameters are the same as a Python print

4ba41afb   Alain Klotz   Debut d'integrati...
364
365
    a = 2
    b = 'tutu'
4f2a9fef   Alain Klotz   Ameliorations des...
366
    log.print(f"a={a} b={b}")
1bb025d7   Alain Klotz   Mise en oeuvre de...
367
    
4f2a9fef   Alain Klotz   Ameliorations des...
368
    log.print(a,b)
1bb025d7   Alain Klotz   Mise en oeuvre de...
369