Skip to content
This repository has been archived by the owner on May 22, 2019. It is now read-only.

Commit

Permalink
Added a new logging formatter that will store resources usage
Browse files Browse the repository at this point in the history
  • Loading branch information
karasu committed Oct 11, 2018
1 parent d6e49e3 commit 259b92b
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[2018-10-11 09:26:40 +0200] Working on logging resources karasu {d6e49e38}
18 changes: 15 additions & 3 deletions src/cnchi.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,16 +296,27 @@ def setup_logging(self):
"($BOLD%(filename)s$RESET:%(lineno)d)")
color_formatter = logging_color.ColoredFormatter(color_fmt, datefmt)

# File logger
log_path = os.path.join(CnchiInit.LOG_FOLDER, 'cnchi.log')
try:
# File logger
log_path = os.path.join(CnchiInit.LOG_FOLDER, 'cnchi.log')
with misc.raised_privileges():
file_handler = logging.FileHandler(log_path, mode='w')
file_handler.setLevel(log_level)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)

# Resources log (debugging purposes)
if self.cmd_line.logresources:
import logging_resources
log_path = os.path.join(CnchiInit.LOG_FOLDER, 'cnchi-resources.log')
with misc.raised_privileges():
resources_handler = logging.FileHandler(log_path, mode='w')
resources_handler.setLevel(log_level)
resources_handler.setFormatter(logging_resources.ResourcesFormatter())
logger.addHandler(resources_handler)
except PermissionError as permission_error:
print("Can't open ", log_path, " : ", permission_error)
print("Cannot open ", log_path, " : ", permission_error)


# Stdout logger
if self.cmd_line.verbose:
Expand All @@ -314,6 +325,7 @@ def setup_logging(self):
stream_handler.setLevel(log_level)
stream_handler.setFormatter(color_formatter)
logger.addHandler(stream_handler)


if not BUGSNAG_ERROR:
# Bugsnag logger
Expand Down
2 changes: 1 addition & 1 deletion src/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

""" Set some Cnchi global constants """

CNCHI_VERSION = "0.16.26"
CNCHI_VERSION = "0.16.27"
""" Cnchi version """

CNCHI_WEBSITE = "http://www.antergos.com"
Expand Down
45 changes: 28 additions & 17 deletions src/logging_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,36 @@
# You should have received a copy of the GNU General Public License
# along with Cnchi; If not, see <http://www.gnu.org/licenses/>.

""" Logging to log resources (for debugging purposes only) """
""" Logging handler to log resources (for debugging purposes only) """


"""
import logging
import resource
import time

usage = resource.getrusage(resource.RUSAGE_SELF)
for name, desc in [
('ru_utime', 'User time'),
('ru_stime', 'System time'),
('ru_maxrss', 'Max. Resident Set Size'),
('ru_ixrss', 'Shared Memory Size'),
('ru_idrss', 'Unshared Memory Size'),
('ru_isrss', 'Stack Size'),
('ru_inblock', 'Block inputs'),
('ru_oublock', 'Block outputs'),
]:
print '%-25s (%-10s) = %s' % (desc, name, getattr(usage, name))
"""
class ResourcesFormatter(logging.Formatter):
def __init__(self, fmt=None, datefmt=None):
""" Init base class """
logging.Formatter.__init__(fmt, datefmt)

self.resources = [
('ru_utime', 'User time'),
('ru_stime', 'System time'),
('ru_maxrss', 'Max. Resident Set Size'),
('ru_ixrss', 'Shared Memory Size'),
('ru_idrss', 'Unshared Memory Size'),
('ru_isrss', 'Stack Size'),
('ru_inblock', 'Block inputs'),
('ru_oublock', 'Block outputs')]

def format(self, record):
""" Ignore record and log resources usage """

usage = resource.getrusage(resource.RUSAGE_SELF)

msg = []
template = "{0} {1} = {2}"
for name, desc in self.resources:
msg.append(template.format(desc, name, getattr(usage, name)))
return "\n".join(msg)


2 changes: 1 addition & 1 deletion update.info
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
{"version":"0.16.26","files":[
{"version":"0.16.27","files":[
]}

0 comments on commit 259b92b

Please sign in to comment.