예제 #1
0
파일: Log.c 프로젝트: brc859844/paho.mqtt.c
/**
 * Write the contents of the stored trace to a stream
 * @param dest string which contains a file name or the special strings stdout or stderr
 */
int Log_dumpTrace(char* dest)
{
	FILE* file = NULL;
	ListElement* cur_trace_entry = NULL;
	const int msgstart = 7;
	int rc = -1;
	int trace_queue_index = 0;

	if ((file = Log_destToFile(dest)) == NULL)
	{
		Log(LOG_ERROR, 9, NULL, "trace", dest, "trace entries");
		goto exit;
	}

	fprintf(file, "=========== Start of trace dump ==========\n");
	/* Interleave the log and trace entries together appropriately */
	ListNextElement(trace_buffer, &cur_trace_entry);
	trace_queue_index = start_index;
	if (trace_queue_index == -1)
		trace_queue_index = next_index;
	else
	{
		Log_formatTraceEntry(&trace_queue[trace_queue_index++]);
		if (trace_queue_index == trace_settings.max_trace_entries)
			trace_queue_index = 0;
	}
	while (cur_trace_entry || trace_queue_index != next_index)
	{
		if (cur_trace_entry && trace_queue_index != -1)
		{	/* compare these timestamps */
			if (Log_compareEntries((char*)cur_trace_entry->content, msg_buf) > 0)
				cur_trace_entry = NULL;
		}

		if (cur_trace_entry)
		{
			fprintf(file, "%s\n", &((char*)(cur_trace_entry->content))[msgstart]);
			ListNextElement(trace_buffer, &cur_trace_entry);
		}
		else
		{
			fprintf(file, "%s\n", &msg_buf[7]);
			if (trace_queue_index != next_index)
			{
				Log_formatTraceEntry(&trace_queue[trace_queue_index++]);
				if (trace_queue_index == trace_settings.max_trace_entries)
					trace_queue_index = 0;
			}
		}
	}
	fprintf(file, "========== End of trace dump ==========\n\n");
	if (file != stdout && file != stderr && file != NULL)
		fclose(file);
	rc = 0;
exit:
	return rc;
}
예제 #2
0
/**
 * Dump the broker heap so it can be viewed for debugging.
 * @param dest - stream destination to write the information to
 * @return completion code, success == 0
 */
int Broker_dumpHeap(char* dest)
{
	FILE* file = NULL;
	int rc = 0;

	if ((file = Log_destToFile(dest)) == NULL)
		rc = -1;
	else
	{
		Sockets* sockets = Socket_getSockets();
		int ssize = sizeof(Sockets);
		int bssize = sizeof(BrokerStates);
		int mpsize = sizeof(MQTTProtocol);
		MQTTProtocol* mp = MQTTProtocol_getState();

		fprintf(file, "=========== Start of heap dump ==========\n");
		if (fwrite(&bssize, sizeof(bssize), 1, file) != 1) /* write size of Broker state Structure */
			rc = -1;
		else if (fwrite(&BrokerState, bssize, 1, file) != 1) /* write Broker state Structure */
			rc = -1;
		else if (fwrite(&config, sizeof(config), 1, file) != 1)
			rc = -1;
		else if (fwrite(&ssize, sizeof(ssize), 1, file) != 1)
			rc = -1;
		else if (fwrite(sockets, ssize, 1, file) != 1)
			rc = -1;
		else if (fwrite(&mpsize, sizeof(mpsize), 1, file) != 1)	/* write size of MQTTProtocol state structure */
			rc = -1;
		else if (fwrite(mp, mpsize, 1, file) != 1)	/* write MQTTProtocol state structure */
			rc = -1;
		else if (HeapDumpString(file, config) != 0)
			rc = -1;
		else if (HeapDumpString(file, BrokerState.version) != 0)
			rc = -1;
		else if (HeapDumpString(file, BrokerState.timestamp) != 0)
			rc = -1;
		else if (HeapDumpString(file, BrokerState.persistence_location) != 0)
			rc = -1;
		else
			rc = HeapDump(file);
		fprintf(file, "\n=========== End of heap dump ==========\n\n");
		if (file != stdout && file != stderr)
			fclose(file);
	}
	return rc;
}
예제 #3
0
파일: Broker.c 프로젝트: Frank-KunLi/rsmb
/**
 * Dump the broker heap so it can be viewed for debugging.
 * @param dest - stream destination to write the information to
 * @return completion code, success == 0
 */
int Broker_dumpHeap(char* dest)
{
	FILE* file = NULL;
	int rc = 0;

	if ((file = Log_destToFile(dest)) == NULL)
		rc = -1;
	else
	{
		List* log = Log_getLogBuffer();
		List* trace = Log_getTraceBuffer();
		Sockets* sockets = Socket_getSockets();
		int ssize = sizeof(Sockets);
		int bssize = sizeof(BrokerStates);

		fprintf(file, "=========== Start of heap dump ==========\n");
		if (fwrite(&bssize, sizeof(bssize), 1, file) != 1)
			rc = -1;
		else if (fwrite(&BrokerState, bssize, 1, file) != 1)
			rc = -1;
		else if (fwrite(&log, sizeof(log), 1, file) != 1)
			rc = -1;
		else if (fwrite(&trace, sizeof(trace), 1, file) != 1)
			rc = -1;
		else if (fwrite(&config, sizeof(config), 1, file) != 1)
			rc = -1;
		else if (fwrite(&ssize, sizeof(ssize), 1, file) != 1)
			rc = -1;
		else if (fwrite(sockets, ssize, 1, file) != 1)
			rc = -1;
		else if (HeapDumpString(file, config) != 0)
			rc = -1;
		else if (HeapDumpString(file, BrokerState.version) != 0)
			rc = -1;
		else if (HeapDumpString(file, BrokerState.persistence_location) != 0)
			rc = -1;
		else
			rc = HeapDump(file);
		fprintf(file, "\n=========== End of heap dump ==========\n\n");
		if (file != stdout && file != stderr)
			fclose(file);
	}
	return rc;
}