#ifndef DEBUG_LOGFLAGS_H_
#define DEBUG_LOGFLAGS_H_
/*---------------------------------------------------------------------------*/
/* debug_logflags.h                                                          */
/* copyright (c) innovaphone 2012                                            */
/*                                                                           */
/*---------------------------------------------------------------------------*/

NAMESPACE_BEGIN

#define BYTE_TRAILING_ZEROES(x)\
    (((x) & 0xf) ? (((x) & 0x3) ? (((x) & 0x1) ? 0 : 1) : (((x) & 0x4) ? 2 : 3)) : (((x) & 0x30) ? (((x) & 0x10) ? 4 : 5) : (((x) & 0x40) ? 6 : 7)))

#define WORD_TRAILING_ZEROES(x)\
    (((x) & 0xff) ? BYTE_TRAILING_ZEROES(x) : 8 + BYTE_TRAILING_ZEROES((x) >> 8))

#define DWORD_TRAILING_ZEROES(x)\
    (((x) & 0xffff) ? WORD_TRAILING_ZEROES(x) : 16 + WORD_TRAILING_ZEROES((x) >> 16))

#define QWORD_TRAILING_ZEROES(x)\
    (((x) & 0xffffffff) ? DWORD_TRAILING_ZEROES(x) : 32 + DWORD_TRAILING_ZEROES((x) >> 32))

class UDebugLogFlags
{
public:
    virtual void DebugLogFlagsChanged(class DebugLogFlags * debugLogFlags, const char * appName, const char * appDomain, ulong64 flags, bool noHexdump, UDebugLogFlags * src) = 0;
};

class DebugLogInstance
{
public:
    DebugLogInstance(const char * appName, const char * appDomain, class IInstanceLog * instanceLog, class AppService * appService);
    ~DebugLogInstance();

    char * appName;
    char * appDomain;
    class IInstanceLog * instanceLog;
    class AppService * appService;
};

class DebugLogFlags
{
    ulong64 commonLogFlags;
    bool noHexdump;
    unsigned logInstanceCount;
    unsigned logInstanceAllocated;
    class DebugLogInstance ** logInstances;
    unsigned logFlagsUserCount;
    unsigned logFlagsUserAllocated;
    class UDebugLogFlags ** logFlagsUsers;
    bool getLogActive;

public:
    DebugLogFlags();
    ~DebugLogFlags();

    void AddUser(UDebugLogFlags * logFlagsUser);
    void RemoveUser(UDebugLogFlags * logFlagsUser);
    void Add(const char * appName, const char * appDomain, class IInstanceLog * instanceLog, class AppService * appService = NULL);
    void Set(const char * appName, const char * appDomain, ulong64 flags, bool noHexdump, UDebugLogFlags * src = NULL);
    void GetLogActive(bool active);
    bool Enabled();
    ulong64 CommonLogFlags() { return commonLogFlags; };
    bool NoHexdump() { return noHexdump; }
};

NAMESPACE_END

/*---------------------------------------------------------------------------*/
#endif /* DEBUG_LOGFLAGS_H_ */
