1 /* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17 18 #ifndef CTSAUDIO_FILEUTIL_H 19 #define CTSAUDIO_FILEUTIL_H 20 21 #include <stdarg.h> 22 23 #include <utils/String8.h> 24 #include <utils/threads.h> 25 #include <iostream> 26 #include <fstream> 27 28 /** 29 * Class to write to file and stdout at the same time. 30 * 31 */ 32 class FileUtil { 33 public: 34 /** 35 * create log / report dir 36 * @param dirPath returns path of created dir 37 */ 38 static bool prepare(android::String8& dirPath); 39 40 protected: 41 FileUtil(); 42 virtual ~FileUtil(); 43 44 /** 45 * if fileName is NULL, only stdout output will be supproted 46 */ 47 virtual bool init(const char* fileName); 48 49 virtual bool doPrintf(const char* fmt, ...); 50 /// fileOnly log only to file 51 /// loglevel 0 .., -1 means no log level. 52 virtual bool doVprintf(bool fileOnly, int loglevel, const char *fmt, va_list ap); 53 54 private: 55 // store dirPath to prevent creating multiple times 56 static android::String8 mDirPath; 57 std::ofstream mFile; 58 static const int DEFAULT_BUFFER_SIZE = 1024; 59 // buffer for printf. one line longer than this will be truncated. 60 char* mBuffer; 61 int mBufferSize; 62 android::Mutex mWriteLock; 63 }; 64 65 66 #endif // CTSAUDIO_FILEUTIL_H 67