-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlog.py
52 lines (43 loc) · 1.3 KB
/
log.py
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
"""Logging and run lock stuff."""
import xplat
import time, os, sys
if sys.platform == "win32":
import msvcrt
else:
import fcntl
LOG_TO_FILE = True
LOCK_FILE = os.path.join(xplat.appdata, "airbears_supplicant_lockfile")
lockfile = None
LOG_FILE = os.path.join(xplat.appdata, "airbears_supplicant_log")
if LOG_TO_FILE:
file = open(LOG_FILE, "a")
def log(msg):
formatted = "{0} {1}".format(time.asctime(), msg)
if LOG_TO_FILE:
file.write(formatted + "\n")
file.flush()
os.fsync(file.fileno())
else:
print formatted
def lock(): # This really shouldn't be here
global lockfile
lockfile = open(LOCK_FILE, "w")
try:
if sys.platform == "win32":
msvcrt.locking(lockfile.fileno(), msvcrt.LK_NBLCK, 1) # IOError on failure
else:
fcntl.lockf(lockfile.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
return True # Lock succeeded
except IOError, e:
return False
def unlock():
global lockfile
if lockfile is None: # It happens during development!
return
if sys.platform == "win32":
msvcrt.locking(lockfile.fileno(), msvcrt.LK_UNLCK, 1)
else:
fcntl.lockf(lockfile.fileno(), fcntl.LOCK_UN)
lockfile.close()
os.unlink(LOCK_FILE)
# Unlock happens before os._exit, in ui