forked from aksnzhy/xlearn
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
477 additions
and
397 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
//------------------------------------------------------------------------------ | ||
// Copyright (c) 2016 by contributors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
//------------------------------------------------------------------------------ | ||
|
||
/* | ||
Author: Chao Ma ([email protected]) | ||
This file defines the facilities for format printing. | ||
*/ | ||
|
||
#ifndef XLEARN_BASE_FORMAT_PRINT_H_ | ||
#define XLEARN_BASE_FORMAT_PRINT_H_ | ||
|
||
#include <iostream> | ||
#include <vector> | ||
#include <string> | ||
|
||
#include "src/base/common.h" | ||
|
||
typedef std::vector<std::string> StringList; | ||
typedef std::vector<int> IntList; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Example: | ||
// | ||
// column -> "Name", "ID", "Count", "Price" | ||
// width -> 10, 10, 10, 10 | ||
// | ||
// Output: | ||
// | ||
// Name ID Count Price | ||
// Fruit 0x101 50 5.27 | ||
// Juice 0x102 20 8.73 | ||
// Meat 0x104 30 10.13 | ||
//------------------------------------------------------------------------------ | ||
inline void print_row(const StringList& column, const IntList& width) { | ||
CHECK_EQ(column.size(), width.size()); | ||
for (size_t i = 0; i < column.size(); ++i) { | ||
std::cout.width(width[i]); | ||
std::cout << column[i]; | ||
} | ||
std::cout << "\n"; | ||
} | ||
|
||
//------------------------------------------------------------------------------ | ||
// Example: | ||
// | ||
// std -> "Hello World !" | ||
// | ||
// Output: | ||
// | ||
// ----------------- | ||
// | Hello World ! | | ||
// ----------------- | ||
//------------------------------------------------------------------------------ | ||
inline void print_block(const std::string& str) { | ||
CHECK_NE(str.empty(), true); | ||
// Add two space and two lines | ||
size_t size = str.size() + 4; | ||
for (size_t i = 0; i < size; ++i) { | ||
std::cout << "-"; | ||
} | ||
std::cout << "\n"; | ||
std::cout << "| "; | ||
std::cout << str; | ||
std::cout << " |"; | ||
for (size_t i = 0; i < size; ++i) { | ||
std::cout << "-"; | ||
} | ||
std::cout << "\n"; | ||
} | ||
|
||
#endif // XLEARN_BASE_FORMAT_PRINT_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
//------------------------------------------------------------------------------ | ||
// Copyright (c) 2016 by contributors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
//------------------------------------------------------------------------------ | ||
|
||
/* | ||
Author: Chao Ma ([email protected]) | ||
This file defines several system functions. | ||
*/ | ||
|
||
#ifndef XLEARN_BASE_SYSTEM_H_ | ||
#define XLEARN_BASE_SYSTEM_H_ | ||
|
||
#include <sys/utsname.h> | ||
#include <unistd.h> | ||
|
||
#include <string> | ||
|
||
#include "src/base/common.h" | ||
#include "src/base/stringprint.h" | ||
|
||
// Get host name | ||
std::string get_host_name() { | ||
struct utsname buf; | ||
if (0 != uname(&buf)) { | ||
*buf.nodename = '\0'; | ||
} | ||
return std::string(buf.nodename); | ||
} | ||
|
||
// Get user name | ||
std::string get_user_name() { | ||
const char* username = getenv("USER"); | ||
return username != NULL ? username : getenv("USERNAME"); | ||
} | ||
|
||
// Get current system time | ||
std::string print_current_time() { | ||
time_t current_time = time(NULL); | ||
struct tm broken_down_time; | ||
CHECK(localtime_r(¤t_time, &broken_down_time) == &broken_down_time); | ||
return StringPrintf("%04d%02d%02d-%02d%02d%02d", | ||
1900 + broken_down_time.tm_year, | ||
1 + broken_down_time.tm_mon, | ||
broken_down_time.tm_mday, | ||
broken_down_time.tm_hour, | ||
broken_down_time.tm_min, | ||
broken_down_time.tm_sec); | ||
} | ||
|
||
// The log file name = base + host_name + username + | ||
// date_time + process_id | ||
std::string get_log_file() { | ||
CHECK(!hyper_param_.log_file.empty()); | ||
std::string filename_prefix; | ||
SStringPrintf(&filename_prefix, | ||
"%s.%s.%s.%s.%u", | ||
hyper_param_.log_file.c_str(), | ||
get_host_name().c_str(), | ||
get_user_name().c_str(), | ||
print_current_time().c_str(), | ||
getpid()); | ||
return filename_prefix; | ||
} | ||
|
||
#endif // XLEARN_BASE_SYSTEM_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.