1
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"
14#include <stdio.h>
15
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); }
67#else
68    void nativeLogError(const char msg[]) { fprintf(stderr, "%s", msg); }
69#endif
70    void nativeLogProgress(const char msg[]) { SkDebugf("%s", msg); }
71
72    void fileWrite(const char msg[], size_t size);
73
74    SkFILEWStream* fFileStream;
75};
76
77#endif // SkBenchLogger_DEFINED
78