pmloglib Library API Reference

API Summary

The pmloglib provides the API, which enables applications to format the messages and put these messages to the syslog.

Overview of the API

This library supports instrumenting code to produce runtime text output  to indicate run status.
We differentiate output into two types, tracing and logging.

Tracing code is for detailed debugging for use by individual component development. It is turned on only locally and only as needed, and is not included in the main build or release product. The system that supports for tracing code will not be as efficient in this case as it will lean more towards ease of use.

Logging code, on the other hand, is included in the main build and release product. The purpose is to log errors and important diagnostics information in the device logs. 

Logging code may also support more verbose diagnostic levels which may be turned on dynamically as needed on the device. The intent of the system is that logging code which is not enabled has minimal performance impact. In particular, a logging call which is not enabled should not result in a library call or evaluation of parameters.

This is enabled by requiring logging clients to initialize a logging context that is managed by the PmLog system. The context includes a memory field that can be quickly checked by the logging call to know whether the call is enabled.

The pmloglib service provides the following core features:

  • Context support
  • Filter messages by id
  • Route messages by parameters
  • Provides C API for other components
  • Provide configuration through the pmlogctl utility

The pmloglib supports contexts. Through the pmloglib API, you can configure the contexts, use different files for different contexts and manage the filtering level.

By default, the pmloglib provides 2 contexts:

  • Executable files
  • Dynamic libraries that are used by the executable file

The messages are formatted on the key - value basis. The key serves as a tag, by using which it is possible to get all messages that are marked by a certain key.

Example usage:


    PmLogContext    gMyContext;

    void MyComponentInit(void)
    {
         // Get a pointer to the context for this component.
         // The context is created if it does not already exist.
         PmLogGetContext("PmMyComponent", &gMyContext);
    }

    void MyComponentRun(void)
    {
         int err;

        PmLogDebug(gMyContext, "Calling Foo");
         err = Foo();
         if (err)
         {
            PmLogError(gMyContext, "FOOERR", 1, PMLOGKFV(ERROR_CODE,"%d",err), "");
         }
     }

Below are the list of functions which are exported :

# Exported functions
{
global:
### Public interface (PmLogLib.h) ###
    PmLogGetNumContexts;
    PmLogGetIndContext;
    PmLogFindContext;
    PmLogGetContext;
    PmLogGetContextInline;
    PmLogGetContextName;
    PmLogGetContextLevel;
    PmLogSetContextLevel;
    PmLogPrint_;
    PmLogVPrint_;
    PmLogDumpData_;
    PmLogLevelToString;
    PmLogStringToLevel;
    PmLogFacilityToString;
    PmLogStringToFacility;
    PmLogGetErrDbgString;
    PmLogString_;
    _PmLogMsgKV;
    PmLogGetLibContext;
    PmLogSetLibContext;
   
### Private interface (PmLogLibPrv.h) ###
    PmLogPrvGlobals;
    PmLogPrvLock;
    PmLogPrvUnlock;
    PmLogPrvTest;
    PmLogPrvReadConfigs; 
local:
    *;
};

Enumerators

PmLogErr

 Type definition for error codes returned by this module's methods.

PmLogLevel

Type definition for discrete logging level

Methods

PmLogDumpData_

Description

Logs the specified binary data as text dump to the specified context. Specify kPmLogDumpFormatDefault for the formatting parameter. For efficiency, this method should not be used directly, but instead use the wrappers (e.g., PmLogDumpData, ...) that bypass the library call if the logging is not enabled.

Syntax

PmLogDumpData_(PmLogContext context, PmLogLevel level, const void * data, size_t numBytes, const PmLogDumpFormat * format)

Parameters

Name

Required

Type

Description

contextRequiredPmLogContext

Type definition for the logging context as returned by PmLogGetContext method. This is a read-only data. Clients should treat this as an opaque structure, however it is

referenced by the macro/inline functions here.

levelRequiredPmLogLevel

Type definition for discrete logging level. The values are intentionally identical to the equivalent syslog priority value.

dataRequiredconst void *

Logs the specified binary data as text dump to the specified context

numBytesRequiredsize_t

Size of the input data length

formatRequiredconst PmLogDumpFormat *

Specify kPmLogDumpFormatDefault for the formatting parameter.

currently other format support is not added

Returns

Name

Required

Type

Description

PmLogErrRequiredenum PmLogErr

Type definition for error codes returned by the method. Refer to PmLogErr enumerator for values.

  • Returns InvalidContext if PmLogContext_ handle is null.
  • Returns logErr if PrvCheckContext handle is not proper.
  • Returns NoData, if numbytes is zero.
  • Returns InvalidData, if input data is null. 
  • Returns InvalidFormat if default format is not allowed, when the input format is not equal to NULL.
  • Returns logErr, if it succeeds.

Examples

Example code

This method is used when user wants to logs the specified binary data as text dump to the specified context.

logErr  = PmLogDumpData_(contextName, level,data, numBytes, format)

if (logErr  ==  kPmLogErr_None) {

     ErrPrint("PmLogDumpData_ call  is success);

     return RESULT_OK;

}

PmLogFindContext

Description

Finds the logging context for the named context, or NULL if the context does not exist.

Syntax

PmLogFindContext(const char * contextName, PmLogContext * pContext)

Parameters

Name

Required

Type

Description

contextNameRequiredconst char *

context name 

pContextRequiredPmLogContext *

PmLogContext handle

Returns

Name

Required

Type

Description

PmLogErrRequiredenum PmLogErr

Type definition for error codes returned by the method. Refer to PmLogErr enumerator for values.

  • Returns InvalidParameter, if PmLogContext handle is null.
  • Returns Unknown, if PmLogGlobals handle is null.
  • Returns logErr, if contextName is invalid.
  • Returns None, if PmLogcontext name match found.
  • Returns ContextNotFound, if nothing context name match found.

Examples

Example code

This method is used to find the context name with the previously resolved context name exist or not.

Success Cases:-

logErr = PmLogFindContext(contextName, &context);

if (logErr == kPmLogErr_None) {

     ErrPrint("Context '%s' is already defined.\n", contextName);

     return RESULT_OK;

}

Failure Case:-

logErr = PmLogFindContext(matchContextName, &matchedContext);

if (logErr != kPmLogErr_None)     {

     ErrPrint("Context '%s' not found.\n", matchContextName);

     return RESULT_PARAM_ERR;

}

PmLogGetContext

Description

Logs context for the named context. If contextName is NULL, returns the global context.Context names must be 1~31 characters long, and each character must be one of A-Z, a-z, 0-9, '_', '-', '.'. and Component hierarchies can be indicated using '.' as the path separator. E.g. "FOO.BAR" would indicate the "BAR" subcomponent of the "FOO" component.

Syntax

PmLogGetContext(const char * contextName, PmLogContext * pContext)

Parameters

Name

Required

Type

Description

contextNameRequiredconst char *

Context name

pContextRequiredPmLogContext *

PmLogContext handle

Returns

Name

Required

Type

Description

PmLogErrRequiredenum PmLogErr

Type definition for error codes returned by the method. Refer to PmLogErr enumerator for values.

  • Returns InvalidContextIndex, if contextIndex is invalid.
  • Returns InvalidParameter, if PmLogContext handle is invalid.
  • Returns None, if it succeeds.

Examples

Example code

This method is used to get the current context 

Example 1:

logErr = PmLogGetContext("ASM.LIB", &libContext);

if(logErr == kPmLogErr_None)

{

      PmLogSetLibContext(libContext);

}

Example 2:

logErr = PmLogGetContext(contextName, &context);

    if (logErr != kPmLogErr_None) {

       snprintf(errMsg, errMsgBuffSize, "Error getting context: %s",PmLogGetErrDbgString(logErr));

       return false;

}

PmLogGetContextInline

Description

Gets the logging context for the named context.

Syntax

PmLogGetContextInline(const char * contextName)

Parameters

Name

Required

Type

Description

contextNameRequiredconst char *

PmLogContext name 

Returns

Name

Required

Type

Description

contextRequiredstruct PmLogContext
  • Returns PmLogContext handle if it succeeds.
  • Returns GlobalContext, if current contextName is not available.

Examples

Example code

This method is used to support the trace macros that want to use a named context, but can't easily support a global variable to cache the context.
 

Example 1:

static PmLogContext ctx = PmLogGetContextInline("hbb.avcontrol");

if (NULL == ctx) {

      printf("context name does not exists.")

}
static bool checkDebugMode = false;

PmLogSetContextLevel(ctx, kPmLogLevel_Debug);

 

Example 2:

m_context = PmLogGetContextInline("bootManager");

if (NULL != m_context ) {

      printf("context name  exists.")

}

PmLogGetContextLevel

Description

Gets the logging level for the specified context. This method may be used for the global context. This should generally not be used by the components doing logging themselves. Instead just use the PmLogPrint, etc. methods which will do inline enabled checks.

Syntax

PmLogGetContextLevel(PmLogContext context, PmLogLevel * levelP)

Parameters

Name

Required

Type

Description

contextRequiredPmLogContext

PmLogContext handle

levelPRequiredPmLogLevel *

Type definition for discrete logging level

Returns

Name

Required

Type

Description

PmLogErrRequiredenum PmLogErr

Type definition for error codes returned by the method. Refer to PmLogErr enumerator for values.

  • Returns InvalidContext, if context name is invalid.
  • Returns None, if it succeeds.

Examples

Example code

This method is used to get the logging level for the specified context.
 
PmLogGetContext(contextName, &gmLogContext);

if(gmLogContext != NULL)

{

     logErr = PmLogGetContextLevel(gmLogContext, &logLevel);

     if (logErr == kPmLogErr_None) {

          printf("PmLogGetContextLevel call is success");

     }

}

PmLogGetContextName

Description

Gets the name of the specified context into the specified string buffer.

Syntax

PmLogGetContextName(PmLogContext context, char * contextName, size_t contextNameBuffSize)

Parameters

Name

Required

Type

Description

contextRequiredPmLogContext

PmLogContext handle

contextNameRequiredchar *

PmLogContext name

contextNameBuffSizeRequiredsize_t

Size of PmLogContext buffer

Returns

Name

Required

Type

Description

PmLogErrRequiredenum PmLogErr

Type definition for error codes returned by the method. Refer to PmLogErr enumerator for values.

  • Returns InvalidContext, if context name is invalid.
  • Returns InvalidParameter, if context name or contextNameBuffSize is invalid.
  • Returns BufferTooSmall, if contextNameBuffSize is very small.
  • Returns None, if it succeeds.

Examples

Example code

This method is used to get the name of the specified context into the specified string buffer.
 
logErr = PmLogGetContextName(contextInfoP->context,contextInfoP->contextName, sizeof(contextInfoP->contextName));

if (logErr != kPmLogErr_None){

     return logErr;

}

PmLogGetIndContext

Description

Gets the context by index where index = 0 ~ numContexts-1.

Syntax

PmLogGetIndContext(int contextIndex, PmLogContext * pContext)

Parameters

Name

Required

Type

Description

contextIndexRequiredint

PmLogContext index

pContextRequiredPmLogContext *

Pointer to PmLogContext 

Returns

Name

Required

Type

Description

PmLogErrRequiredenum PmLogErr

Type definition for error codes returned by the method. Refer to PmLogErr enumerator for values.

  • Returns InvalidContextIndex, if contextIndex is invalid.
  • Returns InvalidParameter, if context handle is null.
  • Returns None, if it succeeds.

Examples

Example code

This method is used to get the context by index where index = 0 ~ numContexts-1.

logErr = PmLogGetIndContext(contextIndex, &context);

if(logErr != kPmLogErr_None)

{

     DbgPrint("Context no %d not found. Error no: %d", contextIndex, logErr);

}

PmLogGetNumContexts

Description

Gets the number of defined contexts, including the global context.

Syntax

PmLogGetNumContexts(int * pNumContexts)

Parameters

Name

Required

Type

Description

pNumContextsRequiredint *

Number of PmLogContext 

Returns

Name

Required

Type

Description

PmLogErrRequiredenum PmLogErr

Type definition for error codes returned by the method. Refer to PmLogErr enumerator for values.

  • Returns InvalidParameter, if pNumContexts is null.
  • Returns Unknown, if gGlobalsP is null.
  • Returns None, if it succeeds.

Examples

Example code

This method is used to get the number of defined contexts, including the global context.

Example 1:

logErr = PmLogGetNumContexts(&contextsNumber);

if (logErr != kPmLogErr_None){

     DbgPrint("No contexts found. Error no: %d", logErr);

     return false;

}

 

Example 2:

log_err = PmLogGetNumContexts(&contexts_count);

if (log_err == kPmLogErr_None) {

     printf("PmLogGetNumContexts call is success")

}

PmLogPrint_

Description

Logs the specified formatted text to the specified context.

Syntax

PmLogPrint_(PmLogContext context, PmLogLevel level, const char * fmt)

Parameters

Name

Required

Type

Description

contextRequiredPmLogContext

PmLogContext handle

levelRequiredPmLogLevel

Type definition for discrete logging level.

fmtRequiredconst char *

PmLogContext format string

Returns

Name

Required

Type

Description

PmLogErrRequiredenum PmLogErr

Type definition for error codes returned by the method. Refer to PmLogErr enumerator for values.

  • Returns InvalidContext, if context is invalid.
  • Returns logErr, if context and context level is invalid.
  • Returns None, if it succeeds.

Examples

Example code

This method is used to log the specified formatted text to the specified context.

logErr = PmLogPrint_(context, *levelIntP, "%s", msg);

if (logErr != kPmLogErr_None)

{

     ErrPrint("Error logging: 0x%08X (%s)\n", logErr,PmLogGetErrDbgString(logErr));

     return RESULT_RUN_ERR;

}

PmLogSetContextLevel

Description

Sets the logging level for the specified context. This method may be used for the global context.

Syntax

PmLogSetContextLevel(PmLogContext context, PmLogLevel level)

Parameters

Name

Required

Type

Description

contextRequiredPmLogContext

PmLogContext handle

levelRequiredPmLogLevel

PmLogContext level 

Returns

Name

Required

Type

Description

PmLogErrRequiredenum PmLogErr

Type definition for error codes returned by the method. Refer to PmLogErr enumerator for values.

  • Returns InvalidContext, if handle is invalid.
  • Returns InvalidLevel, if LogLevel is invalid.
  • Returns None, if it succeeds.

Examples

Example code

This method is used to set the logging level for the specified context.

logErr = PmLogSetContextLevel(context, level);

if (logErr != kPmLogErr_None)

{

     snprintf(errMsg, errMsgBuffSize, "Error setting context level: %s",PmLogGetErrDbgString(logErr));

     return false;

}

PmLogString_

Description

Logs the specified string of key-value pairs and free text to the specified context.

Syntax

PmLogString_(PmLogContext context, PmLogLevel level, const char * msgid, const char * kvpairs, const char * message)

Parameters

Name

Required

Type

Description

contextRequiredPmLogContext

PmLogContext handle

levelRequiredPmLogLevel

PmLogContext level 

msgidRequiredconst char *

PmLogContext msgId

kvpairsRequiredconst char *

String of key-value pairs

messageRequiredconst char *

Free text to the specified context

Returns

Name

Required

Type

Description

PmLogErrRequiredenum PmLogErr

Type definition for error codes returned by the method. Refer to PmLogErr enumerator for values.

  • Returns InvalidContext, if context is NULL.
  • Returns InvalidMsgID, if msgid is invalid.
  • Returns InvalidFormat, if string of kvpairs is invalid or if string exceeds 1024 bytes.
  • Returns FormatStringFailed, if formatting of the string is not proper.
  • Returns None, if it succeeds.

Examples

Example code

This method is used to log the specified string of key-value pairs and free text to the specified context.

logErr = PmLogSetContextLevel(context, level);

if (!PmLogIsEnabled(parent_ctx_, level_))

{

     err_ = kPmLogErr_LevelDisabled;

}

else if (level_ == kPmLogLevel_Debug)

{

     logErr = PmLogString_(parent_ctx_, level_, nullptr, nullptr, messages_.c_str());

}

else

{

     logErr = PmLogString_(parent_ctx_, level_,msgId_.c_str(), ("{" + kvpairs_ + "}").c_str(), messages_.c_str());

}

PmLogVPrint_

Description

Logs the specified formatted text to the specified context. For efficiency, this method should not be used directly, but instead use the wrappers (e.g., PmLogVPrint, PmLogVPrintError, ...) that bypass the library call if the logging is not enabled.

Syntax

PmLogVPrint_(PmLogContext context, PmLogLevel level, const char * fmt, va_list args)

Parameters

Name

Required

Type

Description

contextRequiredPmLogContext

PmLogContext Handle

levelRequiredPmLogLevel

Type definition for discrete logging level 

The values are intentionally identical to the equivalent syslog priority value.

fmtRequiredconst char *

Log format string 

argsRequiredva_list

Variable number of arguments

Returns

Name

Required

Type

Description

PmLogErrRequiredPmLogErr

Type definition for error codes returned by the method. Refer to PmLogErr enumerator for values.

  • None
  • InvalidContext
  • InvalidLevel
  • InvalidFormat

Examples

Example code

This method is used to log the specified formatted text to the specified context.

logErr = PmLogVPrint_(context,level,fmt,args)

if (logErr != kPmLogErr_None)

{

     return logErr;

}

PmLogLevelToString

Description

Returns the matching symbolic string for a given numeric level value.

Syntax

PmLogLevelToString(enum PmLogLevel PmLogLevel)

Parameters

Name

Required

Type

Description

PmLogLevelRequiredenum PmLogLevel

 The values are intentionally identical to the equivalent syslog priority value.

Returns

Name

Required

Type

Description

sRequiredconst char *

Returns the matching symbolic string. Otherwise, returns NULL if the level string is not matched.

Examples

Example code

This method is used to convert numeric level value to symbolic string.

s = PmLogLevelToString(level);

if (s != NULL)

{

     return s;

}    

PmLogStringToLevel

Description

Returns the matching numeric value for a given a symbolic level string. 

Syntax

PmLogStringToLevel(const char * levelStr)

Parameters

Name

Required

Type

Description

levelStrRequiredconst char *

Symbolic level string

Returns

Name

Required

Type

Description

nPRequiredint *

Returns the matching numeric value. Otherwise returns NULL if the level string is not matched.

Examples

Example code

This method is used to convert symbolic level string to the numeric value.

s = PmLogStringToLevel(s);

if (s != NULL)

{

     return true;

}

PmLogFacilityToString

Description

Returns the matching symbolic string for a given numeric facility value.

Syntax

PmLogFacilityToString(int facility)

Parameters

Name

Required

Type

Description

facilityRequiredint

Numeric facility value

Returns

Name

Required

Type

Description

sRequiredconst char *

Returns NULL if the facility is invalid. Otherwise returns the matching symbolic string.

Examples

Example code

This method is used to convert the given numeric facility value to the symbolic string.

levelStr = PmLogLevelToString(level);

if (levelStr != NULL)

{

    return true;

}    

PmLogStringToFacility

Description

Returns the matching numeric value for a given symbolic facility string.

Syntax

PmLogStringToFacility(const char * facilityStr)

Parameters

Name

Required

Type

Description

facilityStrRequiredconst char *

Symbolic facility string

Returns

Name

Required

Type

Description

nPRequiredint *

Returns NULL if the facility string is not matched. Otherwise returns the matching numeric value.

Examples

Example code

This method is used to convert the given symbolic facility string to the numeric value.

nP = PmLogStringToFacility(facilityStr);

if (nP != NULL)

{

     return true;

}    

PmLogGetErrDbgString

Description

Returns a matching symbolic string for a given numeric error code value.

Note

  • For debugging only method, never to appear in user interface!

Syntax

PmLogGetErrDbgString(enum PmLogErr logErr)

Parameters

Name

Required

Type

Description

logErrRequiredenum PmLogErr

Numeric error code value

Returns

Name

Required

Type

Description

"?"Requiredconst char *

Always returns a matching symbolic default string.

Examples

Example code

This method is used to convert the given numeric error code value to the symbolic string.

Example 1:

if (logErr != kPmLogErr_None)

{

     ErrPrint("Error getting contexts info: 0x%08X (%s)\n", logErr,PmLogGetErrDbgString(logErr));

     return RESULT_RUN_ERR;

}

Example 2:

snprintf(errMsg, errMsgBuffSize, "Error getting context: %s",PmLogGetErrDbgString(logErr));

_PmLogMsgKV

Description

Logs the specified key-value pair(s) and formatted text tagged at given level, to the specified context.

Syntax

_PmLogMsgKV(struct PmLogContext context, enum PmLogLevel level, unsigned int flags, const char * msgid, size_t kv_count, const char * check_keywords, const char * check_formats, const char * fmt, [char variable no of arguments])

Parameters

Name

Required

Type

Description

contextRequiredstruct PmLogContext

Type definition for the logging context as returned by PmLogGetContext method. This is a read-only data view.

Clients should treat this as an opaque structure, however it is referenced by the macro/inline functions here.

levelRequiredenum PmLogLevel

Type definition for discrete logging level

flagsRequiredunsigned int

Indicates whether to enable or disable the version of backward compatibility. 

msgidRequiredconst char *

Message ID 

kv_countRequiredsize_t

Indicates the number of key value pairs in the message.

check_keywordsRequiredconst char *

Checks for valid keys allowed to be passed that are seperated by one of SOH (start of heading), ASCII char,'\\','" ' ' 

check_formatsRequiredconst char *

Contains kv_count number of '%' chars.

 

fmtRequiredconst char *

Indicates the format of the string. For example, %s ,%c, %d. 

variable no of argumentsOptionalchar

Variable numbers of arguments

Returns

Name

Required

Type

Description

PmLogErrRequiredenum PmLogErr

Type definition for error codes returned by this method

Examples

Example code

This method is used when user wants to log the specified key-value pair(s) and formatted text, tagged at given level, to the specified context.

*(void **)(&dPmLogMsg) = dlsym(hnd, "_PmLogMsgKV");

PmLogGetLibContext

Description

Returns the context of the process loading the library. This method may be used by shared libraries instead of PmLogGetContext method. That is, libraries do not setup their own context, but make this call to invoke the context of the calling process. This makes it easier for processes to control the logging level of library messages and direct them to specified file as needed.

Syntax

PmLogGetLibContext()

Parameters

None

Returns

Name

Required

Type

Description

PmLogContextRequiredstruct PmLogContext

Returns the library context information.

Examples

Example code

This method is used to get the current loaded library context.

error.log(PmLogGetLibContext(), "LS_SUBS_POST_FAIL");

PmLogSetLibContext

Description

Sets PmLog context.

Syntax

PmLogSetLibContext([struct PmLogContext libContext])

Parameters

Name

Required

Type

Description

libContextOptionalstruct PmLogContext

PmLog context information

Returns

None

Examples

Example code

This method is used to set up context for a process where all library messages can be directed.

PmLogSetLibContext(lib_context);

Structures

IntLabel

Define a integer value => string label mapping.

Name

Type

Description

sconst char *

Mapping string 

nint

Mapping interger

PmLogConsole

Settings to control std output

Name

Type

Description

stdErrMinLevelint

Std erro minimum level

stdErrMaxLevelint

Std erro maximum level

stdOutMinLevelint

Std out mininum level

stdOutMaxLevelint

Std out maximum level

PmLogContext

Public context info at the top of the struct so the pointers are the same. If that is changed, revise PmLogGetContext and PrvResolveContextaccordingly.

Name

Type

Description

infoPmLogContextInfo

PmLogContext Info handle

componentchar

PmLogContext component

PmLogGlobals

Global data structure that is allocated as a shared memory segment. The size should be kept reasonable, e.g. < 16K.

Name

Type

Description

signatureuint32_t

Signature of the sahred memory segment

reservedint

Shared memory segment reserved value

maxUserContextsint

Maximum user contexts 

numUserContextsint

Maximum no of user context 

contextLoggingint

PmLogContext logging value

consoleConfPmLogConsole

Settings to control std output

globalContextPmLogContext_

PmLog global context handle

userContextsPmLogContext_

Pmlog user context 

PmLogContextInfo

Type definition for the logging context as returned by PmLogGetContext.  This is a read only data view. Clients should treat this as an opaque structure,

 however it is referenced by the macro/inline functions here

Name

Type

Description

enabledLevelint

levels <= enabledLevel are enabled 

flagsint

enabled or disabled

Contents