/** * Output a variable argument list log string with the specified priority level. * Only defined for Linux and Android * * @param level - DEBUG, INFO, WARNING, ERROR, FATAL * @param tag - Module name * @param format - variadic log string */ void OCLogv(LogLevel level, const char * tag, const char * format, ...) { if (!format || !tag) { return; } char buffer[MAX_LOG_V_BUFFER_SIZE]; memset(buffer, 0, sizeof buffer); va_list args; va_start(args, format); vsnprintf(buffer, sizeof buffer - 1, format, args); va_end(args); OCLog(level, tag, buffer); }
void OIC_HOSTING_LOG(LogLevel level, const char * format, ...) { if (!format) { return; } char buffer[MAX_LOG_V_BUFFER_SIZE] = {}; va_list args; va_start(args, format); vsnprintf(buffer, sizeof buffer - 1, format, args); va_end(args); OCLog(level, "Hosting", buffer); }
CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback, CANetworkPacketReceivedCallback reqRespCallback, CANetworkChangeCallback netCallback) { OCLog(DEBUG, CALEADAPTER_TAG, "IN"); //Input validation VERIFY_NON_NULL(registerCallback, NULL, "RegisterConnectivity callback is null"); VERIFY_NON_NULL(reqRespCallback, NULL, "PacketReceived Callback is null"); VERIFY_NON_NULL(netCallback, NULL, "NetworkChange Callback is null"); #ifdef __TIZEN__ int ret = bt_initialize(); if (0 != ret) { OCLog(ERROR, CALEADAPTER_TAG, "bt_initialize failed!"); return CA_STATUS_FAILED; } #endif //#ifdef __TIZEN__ CASetBLEReqRescallback(reqRespCallback); CALERegisterNetworkNotifications(netCallback); CAConnectivityHandler_t connHandler; connHandler.startListenServer = CAStartLEListeningServer; connHandler.startDiscoverServer = CAStartLEDiscoveryServer; connHandler.sendData = CASendLEUnicastData; connHandler.sendDataToAll = CASendLEMulticastData; connHandler.startNotifyServer = CAStartLENotifyServer; connHandler.sendNotification = CASendLENotification; connHandler.GetnetInfo = CAGetLEInterfaceInformation; connHandler.readData = CAReadLEData; connHandler.terminate = CATerminateLE; registerCallback(connHandler, CA_LE); OCLog(DEBUG, CALEADAPTER_TAG, "OUT"); return CA_STATUS_OK; }
CAResult_t CAStartLEDiscoveryServer() { OCLog(DEBUG, CALEADAPTER_TAG, "IN"); int init_pthread_status = 0; pthread_t pthread_id = 0; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); init_pthread_status = pthread_create(&pthread_id, &attr, CALEClientInitThreadFunc, NULL); if (init_pthread_status != 0) { OCLog(ERROR, CALEADAPTER_TAG, "pthread_create failed!"); return CA_STATUS_FAILED; } OCLog(DEBUG, CALEADAPTER_TAG, "OUT"); pthread_mutex_lock(&gBleIsServerMutex); gIsServer = 0; pthread_mutex_unlock(&gBleIsServerMutex); return CA_STATUS_OK; }
void Tier3StorageMain(Tier3StorageRef me){ do { if(me->doQuitGracefully) { OCObjectLock((OCObjectRef)me->newPLChunks); if(OCListGetCount(me->newPLChunks) == 0 && OCListGetCount(me->toWritePLChunks) == 0) { OCObjectUnlock((OCObjectRef)me->newPLChunks); break; } OCObjectUnlock((OCObjectRef)me->newPLChunks); } OCObjectLock((OCObjectRef)me->newPLChunks); if(OCListGetCount(me->newPLChunks) == 0) { OCObjectUnlock((OCObjectRef)me->newPLChunks); OCThreadSleep(me->thread, 1); continue; } OCListRef _tmp = me->newPLChunks; me->newPLChunks = me->toWritePLChunks; me->toWritePLChunks = _tmp; OCObjectUnlock((OCObjectRef)me->newPLChunks); // do the writing here // NOTE usually we would use a lock file here, // however: we are the only process _writing_ to any file - // the worker tool should only read or delete a file, // since we can write to a deleted file (posix) we are fine // in the case of concurrent access to the same file the worker // may fail on this one, which is ok for us ALPayloadChunkRef plc = NULL; OCAutoreleasePoolCreate(); OCSerializerRef s = (OCSerializerRef)OCAutorelease(OCSerializerCreate()); while( (plc=(ALPayloadChunkRef)OCListFifoPop(me->toWritePLChunks)) != NULL) { OCAutorelease(plc); OCByte* key; size_t keyLength; ALPayloadChunkGetKey(plc, &key, &keyLength); OCByte* keyHexBytes = OCAllocate(ocDefaultAllocator, 2*keyLength+1); if(keyHexBytes == NULL) continue; bzero(keyHexBytes, 2*keyLength+1); OCBytes2Hex(keyHexBytes, key, keyLength); OCStringRef keyStr = (OCStringRef)OC((char*)keyHexBytes); if(keyStr == NULL) { OCDeallocate(ocDefaultAllocator, keyHexBytes); continue; } OCStringRef pathStr = (OCStringRef)OCAutorelease(OCStringCreateAppended(me->path, keyStr)); if(pathStr == NULL) { OCDeallocate(ocDefaultAllocator, keyHexBytes); continue; } ALPayloadChunkSerialize(plc, s); FILE* fp = fopen(CO(pathStr), "a"); if(fp == NULL) { OCLog("Storage: Could not open file at %s", CO(pathStr)); OCDeallocate(ocDefaultAllocator, keyHexBytes); continue; } OCSerializerWriteToStream(s, fp); fclose(fp); OCDeallocate(ocDefaultAllocator, keyHexBytes); OCSerializerReset(s); } OCRelease(&me->toWritePLChunks); OCAutoreleasePoolDestroy(); me->toWritePLChunks = OCListCreate(); } while(1); }
/** * Output a log string with the specified priority level. * Only defined for Linux and Android * * @param level - DEBUG, INFO, WARNING, ERROR, FATAL * @param tag - Module name * @param logStr - log string */ void OCLog(LogLevel level, const char * tag, const char * logStr) { if (!logStr || !tag) { return; } #ifdef __ANDROID__ #ifdef ADB_SHELL printf("%s: %s: %s\n", LEVEL[level], tag, logStr); #else __android_log_write(LEVEL[level], tag, logStr); #endif #elif defined __linux__ || defined __APPLE__ if (logCtx && logCtx->write_level) { logCtx->write_level(logCtx, LEVEL_XTABLE[level], logStr); } else { int min = 0; int sec = 0; int ms = 0; #if defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0 struct timespec when = { .tv_sec = 0, .tv_nsec = 0 }; clockid_t clk = CLOCK_REALTIME; #ifdef CLOCK_REALTIME_COARSE clk = CLOCK_REALTIME_COARSE; #endif if (!clock_gettime(clk, &when)) { min = (when.tv_sec / 60) % 60; sec = when.tv_sec % 60; ms = when.tv_nsec / 1000000; } #else struct timeval now; if (!gettimeofday(&now, NULL)) { min = (now.tv_sec / 60) % 60; sec = now.tv_sec % 60; ms = now.tv_usec * 1000; } #endif printf("%02d:%02d.%03d %s: %s: %s\n", min, sec, ms, LEVEL[level], tag, logStr); } #endif } /** * Output the contents of the specified buffer (in hex) with the specified priority level. * * @param level - DEBUG, INFO, WARNING, ERROR, FATAL * @param tag - Module name * @param buffer - pointer to buffer of bytes * @param bufferSize - max number of byte in buffer */ void OCLogBuffer(LogLevel level, const char * tag, const uint8_t * buffer, uint16_t bufferSize) { if (!buffer || !tag || (bufferSize == 0)) { return; } // No idea why the static initialization won't work here, it seems the compiler is convinced // that this is a variable-sized object. char lineBuffer[LINE_BUFFER_SIZE]; memset(lineBuffer, 0, sizeof lineBuffer); int lineIndex = 0; int i; for (i = 0; i < bufferSize; i++) { // Format the buffer data into a line snprintf(&lineBuffer[lineIndex*3], sizeof(lineBuffer)-lineIndex*3, "%02X ", buffer[i]); lineIndex++; // Output 16 values per line if (((i+1)%16) == 0) { OCLog(level, tag, lineBuffer); memset(lineBuffer, 0, sizeof lineBuffer); lineIndex = 0; } } // Output last values in the line, if any if (bufferSize % 16) { OCLog(level, tag, lineBuffer); } } #endif //__TIZEN__ #endif //ARDUINO #ifdef ARDUINO /** * Initialize the serial logger for Arduino * Only defined for Arduino */ void OCLogInit() { Serial.begin(115200); }