int main(int argc, char **argv)
{
    int err;
    int has_set_log_format = 0;
    int is_clear_log = 0;
    int getLogSize = 0;
    int mode = O_RDONLY;
	int i;
//    const char *forceFilters = NULL;
	struct log_device_t* devices = NULL;
	struct log_device_t* dev;

    g_logformat = (log_format *)log_format_new();

    if (argc == 2 && 0 == strcmp(argv[1], "--test")) {
        logprint_run_tests();
        exit(0);
    }

    if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
        show_help(argv[0]);
        exit(0);
    }

    for (;;) {
        int ret;

        ret = getopt(argc, argv, "cdt:gsf:r:n:v:b:D");

        if (ret < 0) {
            break;
        }

        switch(ret) {
            case 's':
                // default to all silent
                log_add_filter_rule(g_logformat, "*:s");
            break;

            case 'c':
                is_clear_log = 1;
                mode = O_WRONLY;
            break;

            case 'd':
                g_nonblock = true;
            break;

            case 't':
                g_nonblock = true;
                g_tail_lines = atoi(optarg);
            break;


            case 'g':
                getLogSize = 1;
            break;

			case 'b': {
						  char* buf = (char*) malloc(strlen(LOG_FILE_DIR) + strlen(optarg) + 1);
						  if (buf == NULL) {
							  fprintf(stderr,"Can't malloc LOG_FILE_DIR\n");
							  exit(-1);
						  }
						  strcpy(buf, LOG_FILE_DIR);
						  strcat(buf, optarg);
						  //		snprintf(buf, strlen(LOG_FILE_DIR) + strlen(optarg) + 1, "%s%s", LOG_FILE_DIR, optarg);

                if (devices) {
					dev = devices;
                    while (dev->next) {
						dev = dev->next;
					}
					dev->next = (struct log_device_t *)malloc( sizeof(struct log_device_t));
					if (dev->next == NULL) {
						fprintf(stderr,"Can't malloc log_device\n");
						exit(-1);
					}
					dev->next->device = buf;
					dev->next->fd = -1;
					dev->next->printed = false;
					dev->next->queue = NULL;
					dev->next->next = NULL;

                } else {
					devices = (struct log_device_t *)malloc( sizeof(struct log_device_t));
					if (devices == NULL) {
						fprintf(stderr,"Can't malloc log_device\n");
						exit(-1);
					}
					devices->device = buf;
					devices->fd = -1;
					devices->printed = false;
					devices->queue = NULL;
					devices->next = NULL;
                }
                g_dev_count++;
            }
            break;

            case 'f':
                // redirect output to a file

                g_output_filename = optarg;

            break;

            case 'r':
//                if (optarg == NULL) {
//					fprintf(stderr,"optarg == null\n");
 //                  g_log_rotate_size_kbytes = DEFAULT_LOG_ROTATE_SIZE_KBYTES;
 //              } else {
                    //long logRotateSize;
                    //char *lastDigit;

                    if (!isdigit(optarg[0])) {
                        fprintf(stderr,"Invalid parameter to -r\n");
                        show_help(argv[0]);
                        exit(-1);
                    }
                    g_log_rotate_size_kbytes = atoi(optarg);
   //             }
            break;

            case 'n':
                if (!isdigit(optarg[0])) {
                    fprintf(stderr,"Invalid parameter to -r\n");
                    show_help(argv[0]);
                    exit(-1);
                }

                g_max_rotated_logs = atoi(optarg);
            break;

            case 'v':
                err = set_log_format (optarg);
                if (err < 0) {
                    fprintf(stderr,"Invalid parameter to -v\n");
                    show_help(argv[0]);
                    exit(-1);
                }

                has_set_log_format = 1;
            break;

			default:
				fprintf(stderr,"Unrecognized Option\n");
				show_help(argv[0]);
				exit(-1);
			break;
		}
	}

	if (!devices) {
        devices = (struct log_device_t *)malloc( sizeof(struct log_device_t));
		if (devices == NULL) {
			fprintf(stderr,"Can't malloc log_device\n");
			exit(-1);
		}
		devices->device = strdup("/dev/"LOGGER_LOG_MAIN);
		devices->fd = -1;
		devices->printed = false;
		devices->queue = NULL;
		devices->next = NULL;
        g_dev_count = 1;


        int accessmode =
                  (mode & O_RDONLY) ? R_OK : 0
                | (mode & O_WRONLY) ? W_OK : 0;

        // only add this if it's available
	if (0 == access("/dev/"LOGGER_LOG_SYSTEM, accessmode)) {
		devices->next = (struct log_device_t *)malloc( sizeof(struct log_device_t));
		if (devices->next == NULL) {
			fprintf(stderr,"Can't malloc log_device\n");
			exit(-1);
		}
		devices->next->device = strdup("/dev/"LOGGER_LOG_SYSTEM);
		devices->next->fd = -1;
		devices->next->printed = false;
		devices->next->queue = NULL;
		devices->next->next = NULL;
		g_dev_count ++;
	}
	if (0 == access("/dev/"LOGGER_LOG_APPS, accessmode)) {
		devices->next = (struct log_device_t *)malloc( sizeof(struct log_device_t));
		if (devices->next == NULL) {
			fprintf(stderr,"Can't malloc log_device\n");
			exit(-1);
		}
		devices->next->device = strdup("/dev/"LOGGER_LOG_APPS);
		devices->next->fd = -1;
		devices->next->printed = false;
		devices->next->queue = NULL;
		devices->next->next = NULL;
		g_dev_count ++;
	}
/*
        // only add this if it's available
	int fd;
	if ((fd = open("/dev/"LOGGER_LOG_SYSTEM, mode)) != -1) {
		devices->next = (struct log_device_t *)malloc( sizeof(struct log_device_t));
		devices->next->device = strdup("/dev/"LOGGER_LOG_SYSTEM);
		devices->next->fd = -1;
		devices->next->printed = false;
		devices->next->queue = NULL;
		devices->next->next = NULL;
		g_dev_count ++;

		close(fd);
        }
*/
    }

    if (g_log_rotate_size_kbytes != 0 && g_output_filename == NULL)
	{
		fprintf(stderr,"-r requires -f as well\n");
		show_help(argv[0]);
		exit(-1);
	}

    setup_output();


	if (has_set_log_format == 0) {
		err = set_log_format("brief");
	}
/*
		const char* logFormat = getenv("DLOG_PRINTF_LOG");

	        if (logFormat != NULL) {
			err = set_log_format("brief");

			if (err < 0) {
				fprintf(stderr, "invalid format in DLOG_PRINTF_LOG '%s'\n", logFormat);
			}
		}
	}
	if (forceFilters) {
		err = log_add_filter_string(g_logformat, forceFilters);
		if (err < 0) {
			fprintf (stderr, "Invalid filter expression in -logcat option\n");
			exit(0);
		}
	} else if (argc == optind) {
        // Add from environment variable
		char *env_tags_orig = getenv("DLOG_LOG_TAGS");

		if (env_tags_orig != NULL) {
			err = log_add_filter_string(g_logformat, env_tags_orig);

			if (err < 0) {
				fprintf(stderr, "Invalid filter expression in DLOG_LOG_TAGS\n");
				show_help(argv[0]);
				exit(-1);
			}
		}
	} else {
        // Add from commandline
*/
	fprintf(stderr,"arc = %d, optind = %d ,Kb %d, rotate %d\n", argc, optind,g_log_rotate_size_kbytes,g_max_rotated_logs);

	if(argc == optind )
	{
		// Add from environment variable
        //char *env_tags_orig = getenv("DLOG_TAGS");
		log_add_filter_string(g_logformat, "*:d");
	}
	else
	{

		for (i = optind ; i < argc ; i++) {
			err = log_add_filter_string(g_logformat, argv[i]);

			if (err < 0) {
				fprintf (stderr, "Invalid filter expression '%s'\n", argv[i]);
				show_help(argv[0]);
				exit(-1);
			}
		}
	}
/*
    }
*/
    dev = devices;
    while (dev) {
        dev->fd = open(dev->device, mode);
        if (dev->fd < 0) {
            fprintf(stderr, "Unable to open log device '%s': %s\n",
                dev->device, strerror(errno));
            exit(EXIT_FAILURE);
        }

        if (is_clear_log) {
            int ret;
            ret = clear_log(dev->fd);
            if (ret) {
                perror("ioctl");
                exit(EXIT_FAILURE);
            }
        }

        if (getLogSize) {
            int size, readable;

            size = get_log_size(dev->fd);
            if (size < 0) {
                perror("ioctl");
                exit(EXIT_FAILURE);
            }

            readable = get_log_readable_size(dev->fd);
            if (readable < 0) {
                perror("ioctl");
                exit(EXIT_FAILURE);
            }

            printf("%s: ring buffer is %dKb (%dKb consumed), "
                   "max entry is %db, max payload is %db\n", dev->device,
                   size / 1024, readable / 1024,
                   (int) LOGGER_ENTRY_MAX_LEN, (int) LOGGER_ENTRY_MAX_PAYLOAD);
        }

        dev = dev->next;
    }

    if (getLogSize) {
        return 0;
    }

    if (is_clear_log) {
        return 0;
    }

    read_log_lines(devices);

    return 0;
}
Exemple #2
0
/**
 * @brief	
 * @param	
 * @see		
 * @remarks	
 * @code		
 * @endcode	
 * @return	
**/
int _tmain(int argc, _TCHAR* argv[])
{
	bool ret = false;
	UINT32 _pass_count = 0;
	UINT32 _fail_count = 0;

    set_log_format(false, false, true);

	//assert_bool(true, test_boost_asio_timer);
	//assert_bool(true, test_for_each);

	//assert_bool(true, test_asm_func);
	//assert_bool(true, test_x64_calling_convension);
	//assert_bool(true, test_2_complement);
	//assert_bool(true , test_print_64int);
	//assert_bool(true, test_std_string_find_and_substr);
	//assert_bool(true, test_to_lower_uppper_string);
	////assert_bool(true, test_const_position);		// 컴파일 불가 테스트
	//assert_bool(true, test_initialize_string);
	//assert_bool(true, test_process_tree);
	//assert_bool(true, test_base64);
	//assert_bool(true, test_random);
	assert_bool(true, test_get_local_ip_list);

	//assert_bool(true, test_cpp_class);
	//
	//assert_bool(true, test_nt_name_to_dos_name);
	//assert_bool(true, test_query_dos_device);
	//assert_bool(true, test_get_filepath_by_handle);
	//assert_bool(true, test_bin_to_hex);
	//assert_bool(true, test_str_to_xxx);
	//assert_bool(true, test_set_get_file_position);
	//assert_bool(true, test_get_module_path);
	//assert_bool(true, test_dump_memory);
	//assert_bool(true, test_get_process_name_by_pid);
	//assert_bool(true, test_get_environment_value);
 //   assert_bool(true, test_get_local_ip_list);

	//assert_bool(true, test_rc4_encrypt);
	//
	//assert_bool(true, boost_lexical_cast);
	//assert_bool(true, boost_shared_ptr_void);
	//assert_bool(true, boost_shared_ptr_handle_01);
	//assert_bool(true, boost_shared_ptr_handle_02);
	//assert_bool(true, boost_shared_ptr_handle_03);
	//assert_bool(true, boost_tuple);

	//assert_bool(true, boost_format);

	//assert_bool(true, boost_bind);
	//assert_bool(true, boost_bind2);
	//assert_bool(true, boost_bind3);
	//assert_bool(true, boost_bind4);
	//assert_bool(true, boost_bind5);

	//assert_bool(true, test_std_map);
	//assert_bool(true, test_map_plus_algorithm_1);
	//assert_bool(true, test_map_plus_algorithm_2);
	//assert_bool(true, test_map_plus_algorithm_3);
	//assert_bool(true, test_map_plus_algorithm_4);

	//assert_bool(true, test_registry_util);

	log_info
		"-------------------------------------------------------------------------------"
	log_end

	log_info
		"total test = %u, pass = %u, fail = %u", 
		_pass_count + _fail_count, 
		_pass_count, 
		_fail_count
	log_end

	con_info "press any key to terminate..." con_end
	getchar();
	return 0;
}