Index: cmake/Modules/GetVersionWithHg.cmake
===================================================================
--- cmake/Modules/GetVersionWithHg.cmake	(revision 1159)
+++ cmake/Modules/GetVersionWithHg.cmake	(revision 1159)
@@ -0,0 +1,25 @@
+# This file is called at build time. It regenerates the version.h file based on the hg version.
+
+message(STATUS "Calling script")
+
+EXECUTE_PROCESS(
+	COMMAND ${HGCOMMAND} id -i
+	WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
+	RESULT_VARIABLE reshash
+	OUTPUT_VARIABLE verhash
+	ERROR_QUIET
+	OUTPUT_STRIP_TRAILING_WHITESPACE)
+EXECUTE_PROCESS(
+	COMMAND ${HGCOMMAND} id -n
+	WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
+	RESULT_VARIABLE resval
+	OUTPUT_VARIABLE verval
+	ERROR_QUIET
+	OUTPUT_STRIP_TRAILING_WHITESPACE)
+	
+if (reshash EQUAL 0) 
+	SET(FD_PROJECT_VERSION_HG "${verval}(${verhash})")
+ 	message(STATUS "Source version: ${FD_PROJECT_VERSION_HG}")
+endif (reshash EQUAL 0)
+
+CONFIGURE_FILE(${SRC} ${DST})
Index: freeDiameterd/main.c
===================================================================
--- freeDiameterd/main.c	(revision 1134)
+++ freeDiameterd/main.c	(revision 1159)
@@ -111,21 +111,5 @@
 static void main_version_core(void)
 {
-	printf("%s, version %d.%d.%d"
-#ifdef HG_VERSION
-		" (r%s"
-# ifdef PACKAGE_HG_REVISION
-		"/%s"
-# endif /* PACKAGE_HG_VERSION */
-		")"
-#endif /* HG_VERSION */
-		" (libfdcore: %s)\n", 
-		FD_PROJECT_NAME, FD_PROJECT_VERSION_MAJOR, FD_PROJECT_VERSION_MINOR, FD_PROJECT_VERSION_REV
-#ifdef HG_VERSION
-		, HG_VERSION
-# ifdef PACKAGE_HG_REVISION
-		, PACKAGE_HG_REVISION
-# endif /* PACKAGE_HG_VERSION */
-#endif /* HG_VERSION */
-		, fd_core_version());
+	printf("%s, version %s\n", FD_PROJECT_NAME, fd_core_version);
 }
 
Index: include/freeDiameter/CMakeLists.txt
===================================================================
--- include/freeDiameter/CMakeLists.txt	(revision 1136)
+++ include/freeDiameter/CMakeLists.txt	(revision 1159)
@@ -187,4 +187,31 @@
 ##########################
 
+# Additional hg version when relevant, stored in version.h
+if (EXISTS "${CMAKE_SOURCE_DIR}/.hg")
+	# Search for hg binary to use
+	FIND_PROGRAM(HGCOMMAND hg)
+	if (HGCOMMAND)
+		# Ok, add the custom target so that hg is executed at every build
+		ADD_CUSTOM_TARGET(version_information
+				   COMMAND ${CMAKE_COMMAND} -D HGCOMMAND="${HGCOMMAND}" -D SRC="${CMAKE_CURRENT_SOURCE_DIR}/version.h.in" -D DST="${CMAKE_CURRENT_BINARY_DIR}/version.h" -P "${CMAKE_SOURCE_DIR}/cmake/Modules/GetVersionWithHg.cmake"
+				   DEPENDS "${CMAKE_SOURCE_DIR}/.hg/dirstate"
+				   WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
+				   COMMENT "Retrieving version of the hg repository"
+				   )
+	else (HGCOMMAND)
+		# Display at least "unknown" rev in this case
+		SET(FD_PROJECT_VERSION_HG "unknown")
+		CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/version.h.in ${CMAKE_CURRENT_BINARY_DIR}/version.h)
+		ADD_CUSTOM_TARGET(version_information DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/version.h)
+	endif(HGCOMMAND)
+else (EXISTS "${CMAKE_SOURCE_DIR}/.hg")
+	# We use the pure version number without extension
+	CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/version.h.in ${CMAKE_CURRENT_BINARY_DIR}/version.h)
+	ADD_CUSTOM_TARGET(version_information DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/version.h)
+endif (EXISTS "${CMAKE_SOURCE_DIR}/.hg")
+
+
+##########################
+
 # LFDPROTO_LIBS = libraries required by the libfdproto.
 SET(LFDPROTO_LIBS ${CLOCK_GETTIME_LIBS} ${CMAKE_THREAD_LIBS_INIT} ${IDNA_LIBRARIES} PARENT_SCOPE)
Index: include/freeDiameter/libfdcore.h
===================================================================
--- include/freeDiameter/libfdcore.h	(revision 1155)
+++ include/freeDiameter/libfdcore.h	(revision 1159)
@@ -61,6 +61,5 @@
 		CHECK_PRELUDE(__call__);								\
 		if (__ret__ < 0) {									\
-			__ret__ = errno;								\
-			LOG(faillevel, "ERROR: in '%s' :\t%s",  #__call__ ,  gnutls_strerror(__ret__)); \
+			LOG(faillevel, "TLS ERROR: in '%s' :\t%s",  #__call__ ,  gnutls_strerror(__ret__)); \
 			__fallback__;									\
 		}											\
@@ -76,12 +75,6 @@
 #ifndef EXCLUDE_DEPRECATED
 /* Macro for transition, replace with CHECK_GNUTLS_GEN */
-#define CHECK_GNUTLS_DO( __call__, __fallback__ ) {						        \
-		CHECK_PRELUDE(__call__);								\
-		if (__ret__ < 0) {									\
-			__ret__ = errno;								\
-			TRACE_ERROR("ERROR: in '%s' :\t%s",  #__call__ ,  gnutls_strerror(__ret__));    \
-			__fallback__;									\
-		}											\
-}
+#define CHECK_GNUTLS_DO( __call__, __fallback__ )	\
+	CHECK_GNUTLS_GEN( FD_LOG_ERROR, __call__, __fallback__  )
 
 #endif /* EXCLUDE_DEPRECATED */
@@ -96,6 +89,6 @@
 int fd_core_initialize(void);
 
-/* Return a string describing the version of the library */
-const char *fd_core_version(void);
+/* A string describing the version of the library */
+extern const char fd_core_version[];
 
 /* Parse the freeDiameter.conf configuration file, load the extensions */
Index: include/freeDiameter/libfdproto.h
===================================================================
--- include/freeDiameter/libfdproto.h	(revision 1134)
+++ include/freeDiameter/libfdproto.h	(revision 1159)
@@ -123,4 +123,6 @@
 void fd_libproto_fini(void);
 
+/* Retrieve the version of the binary */
+extern const char fd_libproto_version[];
 
 /*============================================================*/
Index: include/freeDiameter/version.h.in
===================================================================
--- include/freeDiameter/version.h.in	(revision 1159)
+++ include/freeDiameter/version.h.in	(revision 1159)
@@ -0,0 +1,3 @@
+/* Following variable is expended at build time bbased on the result of "hg id" command */
+#cmakedefine FD_PROJECT_VERSION_HG
+#define FD_PROJECT_VERSION_HG_VAL "@FD_PROJECT_VERSION_HG@"
Index: libfdcore/CMakeLists.txt
===================================================================
--- libfdcore/CMakeLists.txt	(revision 1077)
+++ libfdcore/CMakeLists.txt	(revision 1159)
@@ -35,4 +35,5 @@
 	server.c
 	tcp.c
+	version.c
 	)
 
@@ -53,7 +54,9 @@
 # Include path
 INCLUDE_DIRECTORIES(${LFDCORE_INCLUDES})
+INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
 
 # Build the executable
 ADD_LIBRARY(libfdcore SHARED ${FDCORE_SRC} ${FDCORE_GEN_SRC})
+ADD_DEPENDENCIES(libfdcore version_information)
 
 # Avoid the liblib name, and set the version
Index: libfdcore/core.c
===================================================================
--- libfdcore/core.c	(revision 1142)
+++ libfdcore/core.c	(revision 1159)
@@ -166,10 +166,4 @@
 /* Public interfaces */
 
-/* Return a string describing the version of the library */
-const char *fd_core_version(void)
-{
-	return _stringize(FD_PROJECT_VERSION_MAJOR) "." _stringize(FD_PROJECT_VERSION_MINOR) "." _stringize(FD_PROJECT_VERSION_REV);
-}
-
 /* Initialize the libfdcore internals. This also initializes libfdproto */
 int fd_core_initialize(void)
@@ -189,8 +183,8 @@
 	}
 	
-	LOG_N("libfdproto initialized.");
-	
 	/* Name this thread */
 	fd_log_threadname("Main");
+	
+	LOG_N("libfdproto '%s' initialized.", fd_libproto_version);
 	
 	/* Initialize gcrypt and gnutls */
@@ -227,4 +221,7 @@
 	core_state_set(CORE_LIBS_INIT);
 	
+	LOG_N("libfdcore '%s' initialized.", fd_core_version);
+	
+	
 	/* Next thing is to parse the config, leave this for a different function */
 	return 0;
Index: libfdcore/version.c
===================================================================
--- libfdcore/version.c	(revision 1159)
+++ libfdcore/version.c	(revision 1159)
@@ -0,0 +1,47 @@
+/*********************************************************************************************************
+* Software License Agreement (BSD License)                                                               *
+* Author: Sebastien Decugis <sdecugis@freediameter.net>							 *
+*													 *
+* Copyright (c) 2013, WIDE Project and NICT								 *
+* All rights reserved.											 *
+* 													 *
+* Redistribution and use of this software in source and binary forms, with or without modification, are  *
+* permitted provided that the following conditions are met:						 *
+* 													 *
+* * Redistributions of source code must retain the above 						 *
+*   copyright notice, this list of conditions and the 							 *
+*   following disclaimer.										 *
+*    													 *
+* * Redistributions in binary form must reproduce the above 						 *
+*   copyright notice, this list of conditions and the 							 *
+*   following disclaimer in the documentation and/or other						 *
+*   materials provided with the distribution.								 *
+* 													 *
+* * Neither the name of the WIDE Project or NICT nor the 						 *
+*   names of its contributors may be used to endorse or 						 *
+*   promote products derived from this software without 						 *
+*   specific prior written permission of WIDE Project and 						 *
+*   NICT.												 *
+* 													 *
+* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
+* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
+* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
+* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 	 *
+* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 	 *
+* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
+* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF   *
+* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.								 *
+*********************************************************************************************************/
+
+#include <fdcore-internal.h>
+#include <freeDiameter/version.h>
+
+#ifdef FD_PROJECT_VERSION_HG
+# define VERSION \
+	_stringize(FD_PROJECT_VERSION_MAJOR) "." _stringize(FD_PROJECT_VERSION_MINOR) "." _stringize(FD_PROJECT_VERSION_REV) "-" FD_PROJECT_VERSION_HG_VAL
+#else
+# define VERSION \
+	_stringize(FD_PROJECT_VERSION_MAJOR) "." _stringize(FD_PROJECT_VERSION_MINOR) "." _stringize(FD_PROJECT_VERSION_REV)
+#endif
+
+const char fd_core_version[] = VERSION;
Index: libfdproto/CMakeLists.txt
===================================================================
--- libfdproto/CMakeLists.txt	(revision 1080)
+++ libfdproto/CMakeLists.txt	(revision 1159)
@@ -1,4 +1,5 @@
 # Name of the subproject
 Project("libfdproto" C)
+
 
 # List of source files for the library
@@ -18,5 +19,7 @@
 	sessions.c
 	utils.c
+	version.c
 	)
+
 
 # Save the list of files for testcases in the core's directory
@@ -28,4 +31,6 @@
 # Build as a shared library
 ADD_LIBRARY(libfdproto SHARED ${LFDPROTO_SRC})
+
+ADD_DEPENDENCIES(libfdproto version_information)
 
 # Avoid the liblib name, and set the version
Index: libfdproto/version.c
===================================================================
--- libfdproto/version.c	(revision 1159)
+++ libfdproto/version.c	(revision 1159)
@@ -0,0 +1,47 @@
+/*********************************************************************************************************
+* Software License Agreement (BSD License)                                                               *
+* Author: Sebastien Decugis <sdecugis@freediameter.net>							 *
+*													 *
+* Copyright (c) 2013, WIDE Project and NICT								 *
+* All rights reserved.											 *
+* 													 *
+* Redistribution and use of this software in source and binary forms, with or without modification, are  *
+* permitted provided that the following conditions are met:						 *
+* 													 *
+* * Redistributions of source code must retain the above 						 *
+*   copyright notice, this list of conditions and the 							 *
+*   following disclaimer.										 *
+*    													 *
+* * Redistributions in binary form must reproduce the above 						 *
+*   copyright notice, this list of conditions and the 							 *
+*   following disclaimer in the documentation and/or other						 *
+*   materials provided with the distribution.								 *
+* 													 *
+* * Neither the name of the WIDE Project or NICT nor the 						 *
+*   names of its contributors may be used to endorse or 						 *
+*   promote products derived from this software without 						 *
+*   specific prior written permission of WIDE Project and 						 *
+*   NICT.												 *
+* 													 *
+* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
+* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
+* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
+* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 	 *
+* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 	 *
+* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
+* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF   *
+* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.								 *
+*********************************************************************************************************/
+
+#include "fdproto-internal.h"
+#include <freeDiameter/version.h>
+
+#ifdef FD_PROJECT_VERSION_HG
+# define VERSION \
+	_stringize(FD_PROJECT_VERSION_MAJOR) "." _stringize(FD_PROJECT_VERSION_MINOR) "." _stringize(FD_PROJECT_VERSION_REV) "-" FD_PROJECT_VERSION_HG_VAL
+#else
+# define VERSION \
+	_stringize(FD_PROJECT_VERSION_MAJOR) "." _stringize(FD_PROJECT_VERSION_MINOR) "." _stringize(FD_PROJECT_VERSION_REV)
+#endif
+
+const char fd_libproto_version[] = VERSION;
