Skip to content

Library for fast converting numeric error/status code to the constant name, retrieving detailed description text. Getting a numeric code by constant name. Supports C, C++, Golang, Python. Can be built for Windows, Linux, Mac OS

License

Dan0xE/allstat

 
 

Repository files navigation

AllStat Library

A library for fast converting an error code into the name of the corresponding constant and retrieving the error description text. The reverse conversion from constant name to the error code is also supported.

Purpose

The main purpose of the allstat library is to make the logs generated by programs more readable due to the fact that they contain the names of constants, and not numerical error codes. At the same time, this task is solved without loss of performance so the library uses hash tables to access data.

Example of usage

#include <Windows.h>
#include <AllStat/AllStat.h>

#define LastErrorStr()  AllStat::Winerr2Str(GetLastError()).c_str()
...
HANDLE h = CreateFileA(
    "c:\\some_not_existing_name"
    , GENERIC_READ
    , FILE_SHARE_READ
    , nullptr
    , OPEN_EXISTING
    , FILE_ATTRIBUTE_NORMAL
    , nullptr
  );

  if (h == INVALID_HANDLE_VALUE)
  { 
    printf("CreateFile() failed. Error: %s\n", LastErrorStr());
    return 1;
  }

Output CreateFile() failed. Error: ERROR_FILE_NOT_FOUND

Language support

The library is written on C++ and can be built for Windows, Linux and MacOS as a dynamic or static linking library. At the moment, support for the use of the library by projects in C, C++, Python, Golang has been implemented. A dynamic library can be used from a project in any programming language by calling the exported functions. Write to us if you have any problems connecting the library to your project.

Types of errors

The library currently supports the following types of errors / status codes:

NTSTATUS

Many kernel-mode standard driver routines and driver support routines use the NTSTATUS type for return values. Additionally, drivers provide an NTSTATUS-typed value in an IRP’s IO_STATUS_BLOCK structure when completing IRPs. The NTSTATUS type is defined in Ntdef.h, and system-supplied status codes are defined in Ntstatus.h.

Win32 error

Win32 error codes MUST be in the range 0x0000 to 0xFFFF, although Win32 error codes can be used both in 16-bit fields (such as within the HRESULT type specified in section 2.1) as well as 32-bit fields. Most values also have a default message defined, which can be used to map the value to a human-readable text message; when this is done, the Win32 error code is also known as a message identifier.

HRESULT

HRESULT is a data type used in Windows operating systems, and the earlier IBM/Microsoft OS/2 operating system, to represent error conditions, and warning conditions. The original purpose of HRESULTs was to formally lay out ranges of error codes for both public and Microsoft internal use in order to prevent collisions between error codes in different subsystems of the OS/2 operating system. HRESULTs are numerical error codes. Various bits within an HRESULT encode information about the nature of the error code, and where it came from. HRESULT error codes are most commonly encountered in COM programming, where they form the basis for a standardized COM error handling convention.

HTTP Status Code

Hypertext Transfer Protocol (HTTP) response status codes. Status codes are issued by a server in response to a client’s request made to the server. It includes codes from IETF Request for Comments (RFCs), other specifications, and some additional codes used in some common applications of the HTTP. The first digit of the status code specifies one of five standard classes of responses. The message phrases shown are typical, but any human-readable alternative may be provided.

errno

Integer value, which is returned by system calls and some library functions in the event of an error to indicate what went wrong. errno is defined by the ISO C standard to be a modifiable lvalue of type int, and must not be explicitly declared; errno may be a macro. errno is thread-local; setting it in one thread does not affect its value in any other thread.

Kern Return

Apple Kernel return codes

Ipp Status

The IppStatus constant enumerates the status values returned by the Intel IPP functions, indicating whether the operation is error-free.

Performance

Data access is implemented through hash tables. Thus, the maximum number of loops when searching for a value is the maximum number of elements in the branch of the hash table. For the largest types of errors, this size currently does not exceed 150. And on average, this size is 20-30 elements per branch

Custom build

You can use macros listed below to make a custom build of allstat library. An example of the project which uses custom build of allstat library can be found by following link - custom allstat compile project

AS_BUILD_[GENERATOR]

GENERATOR is one of values of AS_GENERATOR enumerator. Define macro as 0 to exclude error/status code modules from build. if AS_CUSTOM_BUILD macro is not defined, all AS_BUILD_[GENERATOR] assumed to be set to 1

AS_COMPRESS_DESCRIPTION

If AS_NO_DESCRIPTION is not defined, descrition strings are included to binary in compressed form. If description strings are compressed, the data size is five times smaller.

AS_CUSTOM_BUILD

If defined, all error/status modules (except explicitly included via defining AS_BUILD_[GENERATOR]=1) are excluded from the build

AS_ENUM_PREFIX

Prefix for generating for error/status enumerators and error/status constants. A prefix can be defined to fix name conflict.

AS_NO_DESCRIPTION

Description strings are omitted. Is neither AS_NO_DESCRIPTION nor AS_COMPRESS_DESCRIPTION defined Descrition strings are included to binary. Strings are not compressed

Compactness

The data is organized in such a way that it is possible to avoid including all the data when building the application with a link to the static version of AllStat library. For example, if the application references only Errno2Str(), then during linking only the common library modules and data tables referring to errno will be linked to the application. In this case, the maximum size in the data tables of each of the error types take error description strings. The library can be built with defined macro AS_NO_DESCRIPTION. In this case, the description data will be an empty string. It is also possible to store description strings in a compressed form (see the AS_COMPRESS_DESCRIPTION macro). Please note that when referring to some functions (for example, Name2Item() or GetDescriptionStrC()), the data of all tables will be linked to the application. Functions leading to the linking of all data are collected in the LinkAll.cpp module

Examples of usage

The WhatIs service of EfmSoft company is built on the use of the AllStat library. You can also use the ErrorLookup utility (the source code of the utility is included in the examples) instead of the standard utility shipped with Microsoft Visual Studio - errlook.exe.

About

Library for fast converting numeric error/status code to the constant name, retrieving detailed description text. Getting a numeric code by constant name. Supports C, C++, Golang, Python. Can be built for Windows, Linux, Mac OS

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C 54.8%
  • C++ 44.8%
  • Other 0.4%