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