Skip to content

Commit

Permalink
Merge pull request #2 from tri-star/develop
Browse files Browse the repository at this point in the history
merge 0.2 codes.
  • Loading branch information
Tristar committed Aug 17, 2014
2 parents 12fc725 + a9ad8f4 commit 7d50995
Show file tree
Hide file tree
Showing 11 changed files with 255 additions and 108 deletions.
26 changes: 0 additions & 26 deletions CMakeLists.txt

This file was deleted.

24 changes: 24 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
cmake_minimum_required (VERSION 2.6)

project (tigrep_project)

find_package(GTest REQUIRED)
find_package(Boost REQUIRED)

#add library folder prefixed /usr/local
#this project assumes boost,gtest static libraries are located under /usr/local.
link_directories(/usr/local/lib)
link_directories(/usr/local/lib64)

set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/../bin )


#variables for project specific.
set(TIGREP_LINK_LIBRARIES libboost_regex.a libboost_program_options.a)
set(TIGREP_LINK_LIBRARIES_FOR_TEST ${TIGREP_LINK_LIBRARIES} pthread libgtest.a libgtest_main.a )


add_executable(tigrep
tigrep.cc
Expand All @@ -8,3 +27,8 @@ add_executable(tigrep
target_link_libraries(tigrep ${TIGREP_LINK_LIBRARIES})

install(TARGETS tigrep DESTINATION bin)


#tests
enable_testing()
add_subdirectory(tests)
33 changes: 24 additions & 9 deletions src/grep_command.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ namespace tigrep {
static const int OUTPUT_STDOUT = 1; ///Output lines to stdout.
static const int OUTPUT_FILE = 2; ///Output lines to a file.

GrepCommand(std::istream& ist, std::ostream& ost, GrepConfig_t& config) {
GrepCommand(std::istream* ist, std::ostream* ost, GrepConfig_t& config) {
initialize(ist, ost, config);
}

virtual ~GrepCommand() {
}

void initialize(std::istream& ist, std::ostream& ost, GrepConfig_t& config) {
ist_ = &ist;
ost_ = &ost;
void initialize(std::istream* ist, std::ostream* ost, GrepConfig_t& config) {
ist_ = ist;
ost_ = ost;
config_ = config;
status_ = kSTATE_SCAN;
current_line_ = 0;
Expand All @@ -59,27 +59,35 @@ namespace tigrep {
std::string date_string_buffer;
boost::cmatch matches;

while(status_ != kSTATE_END && !ist_->eof()) {
while(status_ != kSTATE_END) {

ist_->getline(read_buffer, buffer_size);
if(ist_->bad()) {
throw std::runtime_error("read file error.");
}
if(ist_->eof() && read_buffer[0] == 0) {
//If EOF found, and current line is empty, stop log scanning.
//(Prevent double empty line sending to ost_ stream)
break;
}
current_line_++;

//現在の行に含まれている時刻を取得する
//Get a date part from current line.
if(!boost::regex_search(read_buffer, matches, config_.pattern)) {
//取得できない場合は現在の状態に応じた処理を実行
//If current line does not include a date part,
//continue processing according to the status_ variable.
dispatch(read_buffer);
continue;
}
//取得できた場合は時刻を元に現在の状態を判定
//If current line includes a date part,
//update status_ variable responding to the date part.
date_string_buffer = matches.str(1);
updateStatus(date_string_buffer);

//現在の状態に応じた処理を実行
//Processing action according to the status_ variable.
dispatch(read_buffer);
}
status_ = kSTATE_END;

delete[] read_buffer;
}
Expand Down Expand Up @@ -125,13 +133,20 @@ namespace tigrep {

switch(status_) {
case kSTATE_SCAN:
if(config_.start_date_time == 0) {
status_ = kSTATE_OUTPUT;
break;
}
//Update status to kSTATE_OUTPUT if date_string exceeds config_.start_date_time.
date_compare.setBaseDateTime(config_.start_date_time);
if(date_compare.compare(date_string.c_str(), config_.format.c_str(), util::DateCompare::kIS_GTE)) {
status_ = kSTATE_OUTPUT;
}
break;
case kSTATE_OUTPUT:
if(config_.end_date_time == 0) {
break;
}
//Update status to kSTATE_END if date_string exceeds config_.end_date_time.
date_compare.setBaseDateTime(config_.end_date_time);
if(date_compare.compare(date_string.c_str(), config_.format.c_str(), util::DateCompare::kIS_GT)) {
Expand Down
6 changes: 3 additions & 3 deletions tests/CMakeLists.txt → src/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ add_executable(run_tests
application_config_validator_test.cc
grep_command_test.cc
util/date_compare_test.cc
../src/application_config_validator.h
../src/grep_command.h
../src/util/date_compare.h
../application_config_validator.h
../grep_command.h
../util/date_compare.h
)
target_link_libraries(run_tests ${TIGREP_LINK_LIBRARIES_FOR_TEST})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

#include "../src/application_config_validator.h"
#include "../application_config_validator.h"

#include <list>
#include <string>
Expand Down
161 changes: 161 additions & 0 deletions src/tests/grep_command_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
#include "../grep_command.h"

#include <gtest/gtest.h>

namespace tigrep {


//-----------------------------------------------------------------------
//check that GrepCommand recognize valid start/end position.
TEST(grep_command, basic) {

std::istringstream ist(
"abcdefg\n"
"[2014-08-08 10:00:00] some-text1\n"
"[2014-08-08 11:00:00] some-text2\n"
"[2014-08-08 12:00:00] some-text3\n"
"[2014-08-08 13:00:00] some-text4\n"
"[2014-08-08 14:00:00] some-text5\n"
);
std::ostringstream ost;

GrepConfig config;

config.pattern = boost::regex("^\\[([0-9\\-:\\s]+?)\\]");
config.format = "%Y-%m-%d %H:%M:%S";
config.start_date_time = 1407463200; //date -d "2014-08-08 11:00:00" +%s
config.end_date_time = 1407470400; //date -d "2014-08-08 13:00:00" +%s
GrepCommand grep_command(&ist, &ost, config);

grep_command.execute();


std::istringstream ist_for_check(ost.str().c_str());
char buffer[4096];
memset(buffer, 0, 4096);

//check grep_command.execute() result.
//expect first line is "2014-08-08 11:00:00"
ist_for_check.getline(buffer, 4096);
ASSERT_STREQ("[2014-08-08 11:00:00] some-text2", buffer);

//expect second line is "2014-08-08 12:00:00"
ist_for_check.getline(buffer, 4096);
ASSERT_STREQ("[2014-08-08 12:00:00] some-text3", buffer);

//expect second line is "2014-08-08 13:00:00"
ist_for_check.getline(buffer, 4096);
ASSERT_STREQ("[2014-08-08 13:00:00] some-text4", buffer);

//expect no lines left.
ist_for_check.getline(buffer, 4096);
ASSERT_STREQ("", buffer);
ASSERT_TRUE(ist_for_check.eof());

}

//-----------------------------------------------------------------------
//check that scan start from first line when "from" parameter was omitted.
TEST(grep_command, omit_from_parameter) {

std::istringstream ist(
"abcdefg\n"
"[2014-08-08 10:00:00] some-text1\n"
"[2014-08-08 11:00:00] some-text2\n"
"[2014-08-08 12:00:00] some-text3\n"
"[2014-08-08 13:00:00] some-text4\n"
"[2014-08-08 14:00:00] some-text5\n"
);
std::ostringstream ost;

GrepConfig config;

config.pattern = boost::regex("^\\[([0-9\\-:\\s]+?)\\]");
config.format = "%Y-%m-%d %H:%M:%S";
config.start_date_time = 0; //0 means that parameter omitted.
config.end_date_time = 1407470400; //date -d "2014-08-08 13:00:00" +%s
GrepCommand grep_command(&ist, &ost, config);

grep_command.execute();


std::istringstream ist_for_check(ost.str().c_str());
char buffer[4096];
memset(buffer, 0, 4096);

//check grep_command.execute() result.
//
// When "from" parameter was omitted, the line that does not include a date part are skipped
// until find the first line that include a date part.
// This is current restriction.
//
ist_for_check.getline(buffer, 4096);
ASSERT_STREQ("[2014-08-08 10:00:00] some-text1", buffer);

//read next line.
ist_for_check.getline(buffer, 4096);
ASSERT_STREQ("[2014-08-08 11:00:00] some-text2", buffer);

//read next line.
ist_for_check.getline(buffer, 4096);
ASSERT_STREQ("[2014-08-08 12:00:00] some-text3", buffer);

//read next line.
ist_for_check.getline(buffer, 4096);
ASSERT_STREQ("[2014-08-08 13:00:00] some-text4", buffer);

//expect no lines left.
ist_for_check.getline(buffer, 4096);
ASSERT_STREQ("", buffer);
ASSERT_TRUE(ist_for_check.eof());
}

//-----------------------------------------------------------------------
//check that scan reaches EOF when "to" parameter was omitted.
TEST(grep_command, omit_to_parameter) {

std::istringstream ist(
"abcdefg\n"
"[2014-08-08 10:00:00] some-text1\n"
"[2014-08-08 11:00:00] some-text2\n"
"[2014-08-08 12:00:00] some-text3\n"
"[2014-08-08 13:00:00] some-text4\n"
"[2014-08-08 14:00:00] some-text5\n"
);
std::ostringstream ost;

GrepConfig config;

config.pattern = boost::regex("^\\[([0-9\\-:\\s]+?)\\]");
config.format = "%Y-%m-%d %H:%M:%S";
config.start_date_time = 1407463200; //date -d "2014-08-08 11:00:00" +%s
config.end_date_time = 0;
GrepCommand grep_command(&ist, &ost, config);

grep_command.execute();


std::istringstream ist_for_check(ost.str().c_str());
char buffer[4096];
memset(buffer, 0, 4096);

//check grep_command.execute() result.
ist_for_check.getline(buffer, 4096);
ASSERT_STREQ("[2014-08-08 11:00:00] some-text2", buffer);

ist_for_check.getline(buffer, 4096);
ASSERT_STREQ("[2014-08-08 12:00:00] some-text3", buffer);

ist_for_check.getline(buffer, 4096);
ASSERT_STREQ("[2014-08-08 13:00:00] some-text4", buffer);

ist_for_check.getline(buffer, 4096);
ASSERT_STREQ("[2014-08-08 14:00:00] some-text5", buffer);

//expect no lines left.
ist_for_check.getline(buffer, 4096);
ASSERT_STREQ("", buffer);
ASSERT_TRUE(ist_for_check.eof());
}

} //namespace
30 changes: 30 additions & 0 deletions src/tests/po_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <iostream>
#include <boost/program_options.hpp>
#include <gtest/gtest.h>

namespace po = boost::program_options;

TEST(program_options, basic) {

po::options_description t("description");

int opt;

t.add_options()
("help", "help message.")
("opt", po::value<int>(&opt), "optimazation")
;

po::variables_map vm;
char* av[2];
av[0] = "aaa";
av[1] = "--help";
po::store(po::parse_command_line(2, av, t), vm);
po::notify(vm);

if(vm.count("help")) {
std::cout << t << std::endl;
}


}
2 changes: 1 addition & 1 deletion tests/unit_tests.cc → src/tests/unit_tests.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <gtest/gtest.h>

#include "../src/log_position_finder.h"
#include "../log_position_finder.h"

#include <sstream>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <gtest/gtest.h>

#include "../../src/util/date_compare.h"
#include "../../util/date_compare.h"

#include <sstream>

Expand Down
Loading

0 comments on commit 7d50995

Please sign in to comment.