Trying to strip project path from __FILE__

Viewed 944

the 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;
}

Always returns "iso_a3" and I don't know why... Basically, what I want to do is defining a LOG-macro that outputs the filename and in the beginning calculates the length of the project's base directory to substract it like this: __FILE__[ _base_directory_length ] so that the output is more readable, to be precise:

Debug.h

#pragma once

static int _base_directory_length = strlen( __FILE__ ) - split( __FILE__, '/' ).back().length();

#define LOG( message ) { \
    printf( "%s(%i): %s ", __FILE__ + _base_directory_length, __LINE__, __func__ ); \
    printf( message ); \
    printf( "\n" ); \
}

Does that make sense :-) ? BTW _base_directory_length = strlen( __FILE__ ) - strlen( "Debug.h" ) does not suffice.

3 Answers
Related