Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
seminar-breakout
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Shashank Suhas
seminar-breakout
Commits
c3effc3c
Commit
c3effc3c
authored
May 18, 2017
by
Yuxin Wu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
unload and close the old FileHandler when logger_dir changes (#273)
parent
93cee9d4
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
12 additions
and
8 deletions
+12
-8
tensorpack/utils/logger.py
tensorpack/utils/logger.py
+12
-8
No files found.
tensorpack/utils/logger.py
View file @
c3effc3c
...
@@ -53,11 +53,13 @@ def get_time_str():
...
@@ -53,11 +53,13 @@ def get_time_str():
# logger file and directory:
# logger file and directory:
global
LOG_
FILE
,
LOG_DI
R
global
LOG_
DIR
,
_FILE_HANDLE
R
LOG_DIR
=
None
LOG_DIR
=
None
_FILE_HANDLER
=
None
def
_set_file
(
path
):
def
_set_file
(
path
):
global
_FILE_HANDLER
if
os
.
path
.
isfile
(
path
):
if
os
.
path
.
isfile
(
path
):
backup_name
=
path
+
'.'
+
get_time_str
()
backup_name
=
path
+
'.'
+
get_time_str
()
shutil
.
move
(
path
,
backup_name
)
shutil
.
move
(
path
,
backup_name
)
...
@@ -65,6 +67,8 @@ def _set_file(path):
...
@@ -65,6 +67,8 @@ def _set_file(path):
hdl
=
logging
.
FileHandler
(
hdl
=
logging
.
FileHandler
(
filename
=
path
,
encoding
=
'utf-8'
,
mode
=
'w'
)
filename
=
path
,
encoding
=
'utf-8'
,
mode
=
'w'
)
hdl
.
setFormatter
(
_MyFormatter
(
datefmt
=
'
%
m
%
d
%
H:
%
M:
%
S'
))
hdl
.
setFormatter
(
_MyFormatter
(
datefmt
=
'
%
m
%
d
%
H:
%
M:
%
S'
))
_FILE_HANDLER
=
hdl
_logger
.
addHandler
(
hdl
)
_logger
.
addHandler
(
hdl
)
_logger
.
info
(
"Argv: "
+
' '
.
join
(
sys
.
argv
))
_logger
.
info
(
"Argv: "
+
' '
.
join
(
sys
.
argv
))
...
@@ -77,7 +81,11 @@ def set_logger_dir(dirname, action=None):
...
@@ -77,7 +81,11 @@ def set_logger_dir(dirname, action=None):
dirname(str): log directory
dirname(str): log directory
action(str): an action of ("k","b","d","n") to be performed. Will ask user by default.
action(str): an action of ("k","b","d","n") to be performed. Will ask user by default.
"""
"""
global
LOG_FILE
,
LOG_DIR
global
LOG_DIR
,
_FILE_HANDLER
if
_FILE_HANDLER
:
# unload and close the old file handler, so that we may safely delete the logger directory
_logger
.
removeHandler
(
_FILE_HANDLER
)
del
_FILE_HANDLER
if
os
.
path
.
isdir
(
dirname
):
if
os
.
path
.
isdir
(
dirname
):
if
not
action
:
if
not
action
:
_logger
.
warn
(
"""
\
_logger
.
warn
(
"""
\
...
@@ -104,8 +112,7 @@ If you're resuming from a previous run you can choose to keep it.""")
...
@@ -104,8 +112,7 @@ If you're resuming from a previous run you can choose to keep it.""")
LOG_DIR
=
dirname
LOG_DIR
=
dirname
from
.fs
import
mkdir_p
from
.fs
import
mkdir_p
mkdir_p
(
dirname
)
mkdir_p
(
dirname
)
LOG_FILE
=
os
.
path
.
join
(
dirname
,
'log.log'
)
_set_file
(
os
.
path
.
join
(
dirname
,
'log.log'
))
_set_file
(
LOG_FILE
)
def
disable_logger
():
def
disable_logger
():
...
@@ -114,13 +121,10 @@ def disable_logger():
...
@@ -114,13 +121,10 @@ def disable_logger():
globals
()[
func
]
=
lambda
x
:
None
globals
()[
func
]
=
lambda
x
:
None
def
auto_set_dir
(
action
=
None
,
overwrite
=
False
):
def
auto_set_dir
(
action
=
None
):
"""
"""
Set log directory to a subdir inside "train_log", with the name being
Set log directory to a subdir inside "train_log", with the name being
the main python file currently running"""
the main python file currently running"""
if
LOG_DIR
is
not
None
and
not
overwrite
:
# dir already set
return
mod
=
sys
.
modules
[
'__main__'
]
mod
=
sys
.
modules
[
'__main__'
]
basename
=
os
.
path
.
basename
(
mod
.
__file__
)
basename
=
os
.
path
.
basename
(
mod
.
__file__
)
set_logger_dir
(
set_logger_dir
(
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment