180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/*
380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Copyright 2012 Google Inc.
480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru *
580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Use of this source code is governed by a BSD-style license that can be
680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * found in the LICENSE file.
780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */
880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#ifndef SkBenchLogger_DEFINED
1080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define SkBenchLogger_DEFINED
1180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkTypes.h"
1380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkString.h"
1480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruclass SkFILEWStream;
1680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/**
1880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Class that allows logging to a file while simultaneously logging to stdout/stderr.
1980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */
2080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruclass SkBenchLogger {
2180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querupublic:
2280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkBenchLogger();
2380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    /**
2580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     * Not virtual, since this class is not intended to be subclassed.
2680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     */
2780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    ~SkBenchLogger();
2880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    /**
3080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     * Specify a file to write progress logs to. Unless this is called with a valid file path,
3180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     * SkBenchLogger will only write to stdout/stderr.
3280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     */
3380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    bool SetLogFile(const char file[]);
3480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    /**
3680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     * Log an error to stderr, taking a C style string as input.
3780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     */
3880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void logError(const char msg[]) { this->nativeLogError(msg); }
3980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    /**
4180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     * Log an error to stderr, taking an SkString as input.
4280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     */
4380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void logError(const SkString& str) { this->nativeLogError(str.c_str()); }
4480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    /**
4680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     * Log the progress of the bench tool to both stdout and the log file specified by SetLogFile,
4780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     * if any, taking a C style string as input.
4880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     */
4980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void logProgress(const char msg[]) {
5080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        this->nativeLogProgress(msg);
5180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        this->fileWrite(msg, strlen(msg));
5280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
5380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
5480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    /**
5580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     * Log the progress of the bench tool to both stdout and the log file specified by SetLogFile,
5680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     * if any, taking an SkString as input.
5780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     */
5880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void logProgress(const SkString& str) {
5980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        this->nativeLogProgress(str.c_str());
6080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        this->fileWrite(str.c_str(), str.size());
6180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
6280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
6380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruprivate:
6480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#ifdef SK_BUILD_FOR_ANDROID
6580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void nativeLogError(const char msg[]) { SkDebugf("%s", msg); }
6680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void nativeLogProgress(const char msg[]) { SkDebugf("%s", msg); }
6780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#else
6880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void nativeLogError(const char msg[]) { fprintf(stderr, "%s", msg); }
6980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void nativeLogProgress(const char msg[]) { printf("%s", msg); }
7080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif
7180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
7280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void fileWrite(const char msg[], size_t size);
7380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
7480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkFILEWStream* fFileStream;
7580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru};
7680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
7780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif // SkBenchLogger_DEFINED
78