1 /*
2  * Copyright 2023, The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of 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,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef CODEC_ERROR_LOG_H_
18 
19 #define CODEC_ERROR_LOG_H_
20 
21 #include <sstream>
22 #include <string>
23 
24 #include <android-base/thread_annotations.h>
25 
26 #include <media/stagefright/foundation/AString.h>
27 
28 namespace android {
29 
30 /**
31  * CodecErrorLog gathers what happened during codec failures, and make them
32  * available to clients for debugging purpose.
33  */
34 class CodecErrorLog {
35 public:
36     CodecErrorLog() = default;
37 
38     /**
39      * Log a line of message.
40      *
41      * \note the message should be readable to developers who may not be
42      *       familiar with MediaCodec internals
43      */
44     void log(const char *tag, const char *message);
45     void log(const char *tag, const std::string &message);
46 
47     /**
48      * Extract the accumulated log as string. This operation clears the log.
49      */
50     std::string extract();
51 
52     /**
53      * Clears the previous log.
54      */
55     void clear();
56 
57 private:
58     mutable std::mutex mLock;
59     std::stringstream mStream GUARDED_BY(mLock);
60 };
61 
62 }  // namespace android
63 
64 #endif  // CODEC_ERROR_LOG_H_
65