Skip to content

Commit b071aba

Browse files
authored
Merge pull request #50 from zfi/demo
Add code block to set Linux logging location.
2 parents 9b84f04 + e11da70 commit b071aba

File tree

1 file changed

+37
-11
lines changed

1 file changed

+37
-11
lines changed

BlocklyLogger.py

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,17 @@
1010

1111
__author__ = 'Jim Ewald'
1212

13-
# Constants
13+
# Platform constants
14+
PLATFORM_LINUX = 'linux2'
1415
PLATFORM_MACOS = 'darwin'
1516
PLATFORM_WINDOWS = 'win32'
1617

18+
# Default log path for each platform
1719
DEFAULT_PATH_MACOS = '/Library/Logs/Parallax'
1820
DEFAULT_PATH_WINDOWS = '/AppData/Local/Parallax'
19-
DEFAULT_PATH_LINUX = '/tmp/'
21+
DEFAULT_PATH_LINUX = '/tmp'
2022

23+
# Resulting path for log file
2124
path = None
2225

2326

@@ -41,13 +44,17 @@ def emit(self, record):
4144
# Set correct log file location
4245
if platform == PLATFORM_MACOS:
4346
logfile_name = __set_macos_logpath(filename)
44-
if logfile_name is None:
45-
disable_filelogging = True
46-
else:
47-
# Set the module-level path
48-
path = logfile_name
4947
elif platform == PLATFORM_WINDOWS:
5048
logfile_name = __set_windows_logpath(filename)
49+
elif platform == PLATFORM_LINUX:
50+
logfile_name = __set_linux_logpath(filename)
51+
52+
# Verify that we have a valid location
53+
if logfile_name is None:
54+
disable_filelogging = True
55+
else:
56+
# Set the module-level path
57+
path = logfile_name
5158

5259
# Create a logger
5360
logger = logging.getLogger('blockly')
@@ -74,6 +81,24 @@ def emit(self, record):
7481
logger.info("Logger has been started.")
7582

7683

84+
# Set the default log file location on a Linux system
85+
def __set_linux_logpath(filename):
86+
user_home = os.path.expanduser('~')
87+
log_path = user_home + DEFAULT_PATH_LINUX
88+
89+
# Does the log directory exist
90+
try:
91+
result = __verify_logpath(log_path)
92+
if result is None and __create_logpath(log_path) is None:
93+
return None
94+
else:
95+
return log_path + '/' + filename
96+
97+
except OSError:
98+
return None
99+
100+
101+
# Set the default log file location on a MacOS system
77102
def __set_macos_logpath(filename):
78103
user_home = os.path.expanduser('~')
79104
log_path = user_home + DEFAULT_PATH_MACOS
@@ -86,13 +111,14 @@ def __set_macos_logpath(filename):
86111
log_path = '/tmp'
87112
result = __verify_logpath(log_path)
88113
if result is None:
89-
return 1
114+
return None
90115

91116
return log_path + '/' + filename
92117
except OSError:
93-
return 2
118+
return None
94119

95120

121+
# Set the default log file location on a Windows system
96122
def __set_windows_logpath(filename):
97123
user_home = os.path.expanduser('~')
98124
log_path = user_home + DEFAULT_PATH_WINDOWS
@@ -107,11 +133,11 @@ def __set_windows_logpath(filename):
107133
result = __verify_logpath(log_path)
108134

109135
if result is None:
110-
return 1
136+
return None
111137

112138
return log_path + '/' + filename
113139
except OSError:
114-
return 2
140+
return None
115141

116142

117143
# Create a file path for the log file

0 commit comments

Comments
 (0)