errno.h 2.91 KB
Newer Older
gejun's avatar
gejun committed
1
// Copyright (c) 2010 Baidu, Inc.
gejun's avatar
gejun committed
2 3 4 5
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
gejun's avatar
gejun committed
6
// 
gejun's avatar
gejun committed
7 8 9 10 11 12 13 14
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
 
gejun's avatar
gejun committed
15 16 17
// Author: Ge,Jun (gejun@baidu.com)
// Date: Fri Sep 10 13:34:25 CST 2010

gejun's avatar
gejun committed
18 19 20 21
// Add customized errno.

#ifndef BAIDU_BASE_BAIDU_ERRNO_H
#define BAIDU_BASE_BAIDU_ERRNO_H
gejun's avatar
gejun committed
22 23 24

#define __const__
#include <errno.h>                           // errno
25
#include "butil/macros.h"                     // BAIDU_CONCAT
gejun's avatar
gejun committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

//-----------------------------------------
// Use system errno before defining yours !
//-----------------------------------------
//
// To add new errno, you shall define the errno in header first, either by
// macro or constant, or even in protobuf.
//
//     #define ESTOP -114                // C/C++
//     static const int EMYERROR = 30;   // C/C++
//     const int EMYERROR2 = -31;        // C++ only
//
// Then you can register description of the error by calling
// BAIDU_REGISTER_ERRNO(the_error_number, its_description) in global scope of
// a .cpp or .cc files which will be linked.
// 
//     BAIDU_REGISTER_ERRNO(ESTOP, "the thread is stopping")
//     BAIDU_REGISTER_ERRNO(EMYERROR, "my error")
//
// Once the error is successfully defined:
//     berror(error_code) returns the description.
//     berror() returns description of last system error code.
//
// %m in printf-alike functions does NOT recognize errors defined by
// BAIDU_REGISTER_ERRNO, you have to explicitly print them by %s.
//
//     errno = ESTOP;
//     printf("Something got wrong, %m\n");            // NO
//     printf("Something got wrong, %s\n", berror());  // YES
//
// When the error number is re-defined, a linking error will be reported:
// 
//     "redefinition of `class BaiduErrnoHelper<30>'"
//
// Or the program aborts at runtime before entering main():
// 
//     "Fail to define EMYERROR(30) which is already defined as `Read-only file system', abort"
//

65
namespace butil {
gejun's avatar
gejun committed
66 67 68 69 70 71 72 73
// You should not call this function, use BAIDU_REGISTER_ERRNO instead.
extern int DescribeCustomizedErrno(int, const char*, const char*);
}

template <int error_code> class BaiduErrnoHelper {};

#define BAIDU_REGISTER_ERRNO(error_code, description)                   \
    const int ALLOW_UNUSED BAIDU_CONCAT(baidu_errno_dummy_, __LINE__) =              \
74
        ::butil::DescribeCustomizedErrno((error_code), #error_code, (description)); \
gejun's avatar
gejun committed
75 76 77 78 79
    template <> class BaiduErrnoHelper<(int)(error_code)> {};

const char* berror(int error_code);
const char* berror();

gejun's avatar
gejun committed
80
#endif  //BAIDU_BASE_BAIDU_ERRNO_H