Revise cpp codes
[platform/core/api/audio-io.git] / src / cpp / CAudioError.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
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 #include <stdio.h>
18 #include <string.h>
19 #include <dlog.h>
20 #include "CAudioIODef.h"
21
22
23 using namespace std;
24 using namespace tizen_media_audio;
25
26
27 /**
28  * class CAudioError
29  */
30 CAudioError::EError CAudioError::__mLastError = CAudioError::EError::ERROR_NONE;
31 char CAudioError::__mLastErrorMsg[MSG_LENGTH];
32
33 //LCOV_EXCL_START
34 CAudioError::CAudioError(EError err) :
35     __mError(err) {
36     __mLastError = __mError;
37 }
38
39 CAudioError::CAudioError(EError err, const char* fileName, const char* parentFunc, int lineNum) :
40     __mError(err) {
41     __mLastError = __mError;
42
43     const char* findFileName = strrchr(fileName, '/');
44     if (findFileName)
45         findFileName++;
46     const char* errStr = __convertErrorToString(__mError);
47
48     snprintf(__mErrorMsg, MSG_LENGTH, "["
49             COLOR_RED    "THROW" COLOR_END ":%s|"
50             COLOR_YELLOW "ERR"    COLOR_END ":%s|"
51             COLOR_YELLOW "FUNC"   COLOR_END ":%s(%d)]", findFileName, errStr, parentFunc, lineNum);
52
53     snprintf(__mLastErrorMsg, MSG_LENGTH, "LastError:%s", __mErrorMsg);
54 }
55 //LCOV_EXCL_STOP
56
57 CAudioError::CAudioError(EError err, const char* msg, const char* fileName, const char* parentFunc, int lineNum) :
58     __mError(err) {
59     __mLastError = __mError;
60
61     const char* findFileName = strrchr(fileName, '/');
62     if (findFileName)
63         findFileName++;
64     const char* errStr = __convertErrorToString(__mError);
65
66     snprintf(__mErrorMsg, MSG_LENGTH, "["
67             COLOR_RED    "THROW" COLOR_END ":%s|"
68             COLOR_YELLOW "ERR"    COLOR_END ":%s|"
69             COLOR_YELLOW "FUNC"   COLOR_END ":%s(%d)]"
70             COLOR_YELLOW "MSG"    COLOR_END ":"
71             COLOR_CYAN   "%s"     COLOR_END, findFileName, errStr, parentFunc, lineNum, msg);
72
73     snprintf(__mLastErrorMsg, MSG_LENGTH, "LastError:%s", __mErrorMsg);
74 }
75
76 //LCOV_EXCL_START
77 CAudioError::CAudioError(const CAudioError& err) {
78     __mError = err.__mError;
79     memcpy(__mErrorMsg, err.__mErrorMsg, MSG_LENGTH);
80 }
81 //LCOV_EXCL_STOP
82
83 const char* CAudioError::__convertErrorToString(EError err) {
84     switch (err) {
85     default:
86     case EError::ERROR_NONE:                return COLOR_GREEN "ERROR_NONE"               COLOR_END;
87     case EError::ERROR_INVALID_ARGUMENT:    return COLOR_RED   "ERROR_INVALID_ARGUMENT"   COLOR_END;
88 //LCOV_EXCL_START
89     case EError::ERROR_INVALID_HANDLE:      return COLOR_RED   "ERROR_INVALID_HANDLE"     COLOR_END;
90     case EError::ERROR_INVALID_SAMPLERATE:  return COLOR_RED   "ERROR_INVALID_SAMPLERATE" COLOR_END;
91     case EError::ERROR_INVALID_CHANNEL:     return COLOR_RED   "ERROR_INVALID_CHANNEL"    COLOR_END;
92     case EError::ERROR_INVALID_FORMAT:      return COLOR_RED   "ERROR_INVALID_FORMAT"     COLOR_END;
93     case EError::ERROR_INVALID_POINTER:     return COLOR_RED   "ERROR_INVALID_POINTER"    COLOR_END;
94 //LCOV_EXCL_STOP
95     case EError::ERROR_INVALID_OPERATION:   return COLOR_RED   "ERROR_INVALID_OPERATION"  COLOR_END;
96     case EError::ERROR_INVALID_STATE:       return COLOR_RED   "ERROR_INVALID_STATE"      COLOR_END;
97     case EError::ERROR_NOT_INITIALIZED:     return COLOR_RED   "ERROR_NOT_INITIALIZED"    COLOR_END;
98     case EError::ERROR_NOT_SUPPORTED:       return COLOR_RED   "ERROR_NOT_SUPPORTED"      COLOR_END;
99     case EError::ERROR_NOT_SUPPORTED_TYPE:  return COLOR_RED   "ERROR_NOT_SUPPORTED_TYPE" COLOR_END;
100 //LCOV_EXCL_START
101     case EError::ERROR_PERMISSION_DENIED:   return COLOR_RED   "ERROR_PERMISSION_DENIED"  COLOR_END;
102     case EError::ERROR_DEVICE_POLICY_RESTRICTION:   return COLOR_RED   "ERROR_DEVICE_POLICY_RESTRICTION"  COLOR_END;
103     case EError::ERROR_DEVICE_NOT_OPENED:   return COLOR_RED   "ERROR_DEVICE_NOT_OPENED"  COLOR_END;
104     case EError::ERROR_DEVICE_NOT_CLOSED:   return COLOR_RED   "ERROR_DEVICE_NOT_CLOSED"  COLOR_END;
105     case EError::ERROR_OUT_OF_MEMORY:       return COLOR_RED   "ERROR_OUT_OF_MEMORY"      COLOR_END;
106     case EError::ERROR_INTERNAL_OPERATION:  return COLOR_RED   "ERROR_INTERNAL_OPERATION" COLOR_END;
107     case EError::ERROR_FAILED_OPERATION:    return COLOR_RED   "ERROR_FAILED_OPERATION"   COLOR_END;
108     case EError::ERROR_POLICY_BLOCKED:      return COLOR_RED   "ERROR_POLICY_BLOCKED"     COLOR_END;
109     case EError::ERROR_POLICY_INTERRUPTED:  return COLOR_RED   "ERROR_POLICY_INTERRUPTED" COLOR_END;
110     case EError::ERROR_POLICY_DUPLICATED:   return COLOR_RED   "ERROR_POLICY_DUPLICATED"  COLOR_END;
111 //LCOV_EXCL_STOP
112     }
113 }
114
115 //LCOV_EXCL_START
116 CAudioError::EError CAudioError::getLastError() {
117     return __mLastError;
118 }
119
120 const char* CAudioError::getLastErrorMsg() {
121     return __mLastErrorMsg;
122 }
123 //LCOV_EXCL_STOP
124
125 CAudioError::EError CAudioError::getError() {
126     return __mError;
127 }
128
129 const char* CAudioError::getErrorMsg() {
130     return __mErrorMsg;
131 }
132
133 //LCOV_EXCL_START
134 CAudioError& CAudioError::operator = (const EError err) {
135     __mError = err;
136     __mLastError = __mError;
137     return *this;
138 }
139
140 CAudioError& CAudioError::operator = (const CAudioError& err) {
141     __mError = err.__mError;
142     __mLastError = __mError;
143     memcpy(__mErrorMsg, err.__mErrorMsg, MSG_LENGTH);
144     memcpy(__mLastErrorMsg, __mErrorMsg, MSG_LENGTH);
145     return *this;
146 }
147
148 bool CAudioError::operator != (const EError err) {
149     return (__mError != err);
150 }
151
152 bool CAudioError::operator == (const EError err) {
153     return (__mError == err);
154 }
155 //LCOV_EXCL_STOP
156
157 //bool operator == (const CAudioError& src, const CAudioError::EError& err) {
158 //    //return (src.__mLastError == err);
159 //    return true;
160 //}