blob: a7c283ad50516a585f0772eb73c2bcd61e1853c1 [file] [log] [blame]
scroggo@google.com9a412522012-09-07 15:21:18 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
tfarinaf168b862014-06-19 12:32:29 -07008#ifndef BenchLogger_DEFINED
9#define BenchLogger_DEFINED
scroggo@google.com9a412522012-09-07 15:21:18 +000010
bungeman@google.comfab44db2013-10-11 18:50:45 +000011#include <stdio.h>
tfarinaf168b862014-06-19 12:32:29 -070012#include "SkString.h"
13#include "SkTypes.h"
scroggo@google.com9a412522012-09-07 15:21:18 +000014
15class SkFILEWStream;
16
17/**
18 * Class that allows logging to a file while simultaneously logging to stdout/stderr.
19 */
tfarinaf168b862014-06-19 12:32:29 -070020class BenchLogger {
scroggo@google.com9a412522012-09-07 15:21:18 +000021public:
tfarinaf168b862014-06-19 12:32:29 -070022 BenchLogger();
scroggo@google.com9a412522012-09-07 15:21:18 +000023
24 /**
25 * Not virtual, since this class is not intended to be subclassed.
26 */
tfarinaf168b862014-06-19 12:32:29 -070027 ~BenchLogger();
scroggo@google.com9a412522012-09-07 15:21:18 +000028
29 /**
30 * Specify a file to write progress logs to. Unless this is called with a valid file path,
tfarinaf168b862014-06-19 12:32:29 -070031 * BenchLogger will only write to stdout/stderr.
scroggo@google.com9a412522012-09-07 15:21:18 +000032 */
33 bool SetLogFile(const char file[]);
34
35 /**
36 * Log an error to stderr, taking a C style string as input.
37 */
38 void logError(const char msg[]) { this->nativeLogError(msg); }
39
40 /**
41 * Log an error to stderr, taking an SkString as input.
42 */
43 void logError(const SkString& str) { this->nativeLogError(str.c_str()); }
44
45 /**
46 * Log the progress of the bench tool to both stdout and the log file specified by SetLogFile,
47 * if any, taking a C style string as input.
48 */
49 void logProgress(const char msg[]) {
50 this->nativeLogProgress(msg);
51 this->fileWrite(msg, strlen(msg));
52 }
53
54 /**
55 * Log the progress of the bench tool to both stdout and the log file specified by SetLogFile,
56 * if any, taking an SkString as input.
57 */
58 void logProgress(const SkString& str) {
59 this->nativeLogProgress(str.c_str());
60 this->fileWrite(str.c_str(), str.size());
61 }
62
63private:
64#ifdef SK_BUILD_FOR_ANDROID
65 void nativeLogError(const char msg[]) { SkDebugf("%s", msg); }
scroggo@google.com9a412522012-09-07 15:21:18 +000066#else
67 void nativeLogError(const char msg[]) { fprintf(stderr, "%s", msg); }
scroggo@google.com9a412522012-09-07 15:21:18 +000068#endif
commit-bot@chromium.orgaadb4d92013-10-09 15:09:42 +000069 void nativeLogProgress(const char msg[]) { SkDebugf("%s", msg); }
scroggo@google.com9a412522012-09-07 15:21:18 +000070
71 void fileWrite(const char msg[], size_t size);
72
73 SkFILEWStream* fFileStream;
74};
75
tfarinaf168b862014-06-19 12:32:29 -070076#endif // BenchLogger_DEFINED