following code (main.cpp):
#include <string>
#include <vector>
std::vector< std::string > split( std::string haystack, const char limiter ) {
std::vector< std::string > return_value;
while( haystack.find( limiter ) != std::string::npos ) {
return_value.push_back( haystack.substr( 0, haystack.find( limiter ) ) );
haystack = haystack.substr( haystack.find( limiter ) + 1 );
}
return_value.push_back( haystack );
return return_value;
}
const char* str = split( std::string( __FILE__ ), '/' ).back().c_str();
int main() {
printf( "%s\n", str );
return 0;
}
It always returns "iso_a3", and I donโt know why ... Basically, what I want to do is define a LOG macro that displays the file name and at the beginning calculates the length of the projectโs base directory to subtract it for example: __FILE__[ _base_directory_length ]to exit was more readable, to be precise:
debug.h
static int _base_directory_length = strlen( __FILE__ ) - split( __FILE__, '/' ).back().length();
printf( "%s(%i): %s ", __FILE__ + _base_directory_length, __LINE__, __func__ ); \
printf( message ); \
printf( "\n" ); \
}
It makes sense:-)? BTW is _base_directory_length = strlen( __FILE__ ) - strlen( "Debug.h" )not enough.
source
share