Index

lognestmonster / ca9821e

A general-purpose single-header C logging library and parser for event-based logs. (Incomplete)

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
9101 Sep 2019 21:35ca9821einfile bufferingJosh Stockin164N

Blob @ lognestmonster / tests / write.py

application/x-python1456 bytesdownload raw
1#!/usr/bin/env python
2
3import sys
4import os
5import time
6import ctypes
7import random
8
9def exists(f):
10 return os.path.isfile(f) or os.path.isdir(f)
11
12def milli():
13 return int(round(time.time() * 1000))
14
15def enc(string):
16 return string.encode("utf-8")
17
18def ushort(n):
19 return ctypes.c_ushort(n)
20
21def uchar(n):
22 return ctypes.c_ubyte(n)
23
24def ulonglong(n):
25 return ctypes.c_ulonglong(n)
26
27def s(n=20):
28 l = int(random.random()*n)
29 st = ""
30 for i in range(0, l):
31 c = 97+int(random.random()*26)
32 st += chr(c)
33 return st
34
35if __name__ == "__main__":
36 try:
37 out = sys.argv[1]
38 except:
39 print("must provide an out location")
40 exit(1)
41
42 version = uchar(1)
43 queue_time = ulonglong(milli())
44 open_statement = uchar(0)
45 close_statement = uchar(1)
46 open_event = uchar(2)
47 close_event = uchar(3)
48
49 verbosity = uchar(0)
50
51 start = milli()
52
53 try:
54 f = open(out, "wb")
55
56 f.write(version)
57 f.write(queue_time)
58 f.write(open_event)
59 for i in range(0, 10000):
60 ts = ulonglong(milli())
61
62 tag = enc(s(10))
63 tag_len = uchar(len(tag))
64
65 message = enc(s(10))
66 message_len = ushort(len(message))
67 f.write(open_event)
68 f.write(open_statement)
69 f.write(ts)
70 f.write(verbosity)
71 f.write(tag_len)
72 f.write(tag)
73 f.write(message_len)
74 f.write(message)
75 f.write(close_statement)
76 f.write(close_event)
77 f.write(close_event)
78
79 finally:
80 f.close()
81 print("file written with size {0} in {1} seconds".format(os.stat(out).st_size, (milli()-start)/1000))
82
83
84