/// <summary> /// Load ini config data from ini format string content. /// </summary> /// <param name="content">ini format string content</param> public void LoadFromContent(string content) { _sections.Clear(); unsafe { int sectionCount = 0; IntPtr nativeContent = LibUtil.CreateNativeStr(content); int errDescLen = 0; IntPtr nativeSections = LLBCNative.csllbc_Ini_LoadFromContent(nativeContent, new IntPtr(§ionCount), new IntPtr(&errDescLen)); LibUtil.FreeNativePtr(ref nativeContent, false); if (sectionCount == -1) { byte * nativeErrorDesc = (byte *)nativeSections; string errorDesc = LibUtil.Ptr2Str(nativeErrorDesc, errDescLen); LibUtil.FreeNativePtr(nativeErrorDesc); throw new LLBCException("{0}", errorDesc); } else if (sectionCount == 0) { return; } _LoadAllSections((_NativeIniSection *)nativeSections.ToPointer(), sectionCount); LLBCNative.csllbc_Ini_FreeNativeSections(nativeSections, sectionCount); } }
private void _Log(object tag, LogLevel level, int skipFrames, string fmt, params object[] args) { if (!enabled) { return; } IntPtr fileName = IntPtr.Zero; // Get filename, lineno, if enabled. int lineNo = -1; if (_enabledLogFileInfo) { var st = new System.Diagnostics.StackTrace(true); var frame = st.GetFrame(skipFrames); lineNo = frame.GetFileLineNumber(); fileName = LibUtil.CreateNativeStr(frame.GetFileName()); } IntPtr nativeTag = IntPtr.Zero; IntPtr nativeMsg = IntPtr.Zero; try { // Get log tag. if (tag is Type) { nativeTag = LibUtil.CreateNativeStr((tag as Type).Name); } else if (tag != null) { nativeTag = LibUtil.CreateNativeStr(tag.ToString()); } // Build log message. nativeMsg = LibUtil.CreateNativeStr(string.Format(fmt, args)); LLBCNative.csllbc_Log_LogMsg(_nativeLogger, fileName, lineNo, (int)level, nativeMsg, nativeTag); } catch (Exception e) { // Firstly, free nativeMsg. System.Runtime.InteropServices.Marshal.FreeHGlobal(nativeMsg); // Simply format error message and dump it. string errMsg = string.Format("Log [{0}] failed, exception:{1}, stacktrace:\n{2}", fmt, e.Message, new System.Diagnostics.StackTrace().ToString()); nativeMsg = LibUtil.CreateNativeStr(errMsg); LLBCNative.csllbc_Log_LogMsg(_nativeLogger, fileName, lineNo, (int)LogLevel.Fatal, nativeMsg, nativeTag); } finally { System.Runtime.InteropServices.Marshal.FreeHGlobal(nativeTag); System.Runtime.InteropServices.Marshal.FreeHGlobal(nativeMsg); } System.Runtime.InteropServices.Marshal.FreeHGlobal(fileName); }
public Logger(string loggerName) { _loggerName = loggerName; IntPtr nativeLoggerName = LibUtil.CreateNativeStr(_loggerName); _nativeLogger = LLBCNative.csllbc_Log_GetLogger(nativeLoggerName); System.Runtime.InteropServices.Marshal.FreeHGlobal(nativeLoggerName); if (_nativeLogger == IntPtr.Zero) { throw ExceptionUtil.CreateExceptionFromCoreLib(); } }
private unsafe void _LoadAllSections(_NativeIniSection *nativeSections, int sectionCount) { for (int sectionIdx = 0; sectionIdx < sectionCount; ++sectionIdx) { _NativeIniSection nativeSection = *(nativeSections + sectionIdx); string sectionName = LibUtil.Ptr2Str(nativeSection.sectionName, nativeSection.sectionNameLen); Dictionary <string, string> values = new Dictionary <string, string>(nativeSection.count); for (int valueIdx = 0; valueIdx < nativeSection.count; ++valueIdx) { string key = LibUtil.Ptr2Str(nativeSection.keys[valueIdx], nativeSection.keysLen[valueIdx]); string value = LibUtil.Ptr2Str(nativeSection.values[valueIdx], nativeSection.valuesLen[valueIdx]); values.Add(key, value); } _sections.Add(sectionName, new IniSection(sectionName, values)); } }
/// <summary> /// Initialize Log. /// </summary> /// <param name="logCfgFile">log config file</param> public static void Init(string logCfgFile) { if (string.IsNullOrEmpty(logCfgFile)) { throw new LLBCException("please specific log config file"); } lock (_lock) { IntPtr nativeStr = LibUtil.CreateNativeStr(logCfgFile); int ret = LLBCNative.csllbc_Log_Init(nativeStr); System.Runtime.InteropServices.Marshal.FreeHGlobal(nativeStr); if (ret != LLBCNative.LLBC_OK) { throw ExceptionUtil.CreateExceptionFromCoreLib(); } _rootLogger = Get("root"); } }
/// <summary> /// Create exception from llbc core library. /// </summary> /// <returns></returns> public static LLBCException CreateExceptionFromCoreLib(uint errNo = 0) { unsafe { int errStrLen = 0; IntPtr errStr; if (errNo == 0) { errStr = LLBCNative.csllbc_FormatLastError(new IntPtr(&errStrLen)); } else { errStr = LLBCNative.csllbc_StrError(errNo, new IntPtr(&errStrLen)); } if (errStrLen > 0) { return(new LLBCException(LibUtil.Ptr2Str(errStr, errStrLen))); } return(new LLBCException("unknown error")); } }