From 9d81b35ebd15a5e1e6fcd1b746e0b9609a60a788 Mon Sep 17 00:00:00 2001 From: kelson42 Date: Tue, 14 Aug 2012 13:08:58 +0000 Subject: [PATCH] + protect the verbose flag with mutex --- src/server/kiwix-serve.cpp | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/server/kiwix-serve.cpp b/src/server/kiwix-serve.cpp index 9f0e27d..662881b 100644 --- a/src/server/kiwix-serve.cpp +++ b/src/server/kiwix-serve.cpp @@ -77,6 +77,7 @@ static pthread_mutex_t welcomeLock = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t searcherLock = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t compressorLock = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t resourceLock = PTHREAD_MUTEX_INITIALIZER; +static pthread_mutex_t verboseFlagLock = PTHREAD_MUTEX_INITIALIZER; // Urlencode //based on javascript encodeURIComponent() @@ -129,6 +130,15 @@ void introduceTaskbar(string &content, const string &humanReadableBookId) { pthread_mutex_unlock(&resourceLock); } +/* Should display debug information? */ +bool isVerbose() { + bool value; + pthread_mutex_lock(&verboseFlagLock); + value = verboseFlag; + pthread_mutex_unlock(&verboseFlagLock); + return value; +} + /* For compression */ #define COMPRESSOR_BUFFER_SIZE 5000000 static Bytef *compr = (Bytef *)malloc(COMPRESSOR_BUFFER_SIZE); @@ -142,6 +152,12 @@ static int accessHandlerCallback(void *cls, const char * upload_data, size_t * upload_data_size, void ** ptr) { + + /* Debug */ + if (isVerbose()) { + std::cout << "Requesting " << url << std::endl; + } + /* Unexpected method */ if (0 != strcmp(method, "GET")) return MHD_NO; @@ -228,7 +244,7 @@ static int accessHandlerCallback(void *cls, pthread_mutex_lock(&searcherLock); try { std::string patternString = string(pattern); - searcher->search(patternString, startNumber, endNumber, verboseFlag); + searcher->search(patternString, startNumber, endNumber, isVerbose()); content = searcher->getHtml(); } catch (const std::exception& e) { std::cerr << e.what() << std::endl; @@ -246,13 +262,13 @@ static int accessHandlerCallback(void *cls, found = reader->getContentByUrl(urlStr, content, contentLength, mimeType); if (found) { - if (verboseFlag) { + if (isVerbose()) { cout << "Found " << urlStr << endl; cout << "content size: " << contentLength << endl; cout << "mimeType: " << mimeType << endl; } } else { - if (verboseFlag) + if (isVerbose()) cout << "Failed to find " << urlStr << endl; content = "Content not found

Not Found

The requested URL " + urlStr + " was not found on this server.

"; @@ -568,6 +584,7 @@ int main(int argc, char **argv) { pthread_mutex_init(&searcherLock, NULL); pthread_mutex_init(&compressorLock, NULL); pthread_mutex_init(&resourceLock, NULL); + pthread_mutex_init(&verboseFlagLock, NULL); /* Start the HTTP daemon */ void *page = NULL; @@ -631,5 +648,6 @@ int main(int argc, char **argv) { pthread_mutex_destroy(&resourceLock); pthread_mutex_destroy(&mapLock); pthread_mutex_destroy(&welcomeLock); + pthread_mutex_destroy(&verboseFlagLock); exit(0); }