dc39f152e836d845123cdafa6e76a6dd4dcb3ffe
[yardstick.git] / yardstick / dispatcher / file.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 import logging
11 import json
12
13 from yardstick.dispatcher.base import Base as DispatchBase
14
15
16 class FileDispatcher(DispatchBase):
17     """Dispatcher class for recording data to a file.
18     """
19
20     __dispatcher_type__ = "File"
21
22     # TODO: make parameters below configurable, currently just hard coded
23     # Path of the file to record the data
24     file_path = "/tmp/yardstick.out"
25     # The max size of the file
26     max_bytes = 0
27     # The max number of the files to keep
28     backup_count = 0
29
30     def __init__(self, conf):
31         super(FileDispatcher, self).__init__(conf)
32         self.log = None
33
34         # if the directory and path are configured, then log to the file
35         if self.file_path:
36             dispatcher_logger = logging.Logger('dispatcher.file')
37             dispatcher_logger.setLevel(logging.INFO)
38             # create rotating file handler which logs result
39             rfh = logging.handlers.RotatingFileHandler(
40                 self.conf.get('file_path', self.file_path),
41                 maxBytes=self.max_bytes,
42                 backupCount=self.backup_count,
43                 encoding='utf8')
44
45             rfh.setLevel(logging.INFO)
46             # Only wanted the data to be saved in the file, not the
47             # project root logger.
48             dispatcher_logger.propagate = False
49             dispatcher_logger.addHandler(rfh)
50             self.log = dispatcher_logger
51
52     def record_result_data(self, data):
53         if self.log:
54             self.log.info(json.dumps(data))