<commit_before>/*
   Copyright 2013 The Trustees of Princeton University

   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.
*/
/*
 * Utility functions (debugging, etc)
 */
 
#include "libsyndicate/util.h"
#include "libsyndicate/libsyndicate.h"

int _SG_DEBUG_MESSAGES = 0;
int _SG_INFO_MESSAGES = 0;

// log all warnings and errors by default
int _SG_WARN_MESSAGES = 1;
int _SG_ERROR_MESSAGES = 1;


void md_set_debug_level( int d ) {
   if( d <= 0 ) {
      // no debugging
      _SG_DEBUG_MESSAGES = 0;
      _SG_INFO_MESSAGES = 0;
   }
   if( d >= 1 ) {
      _SG_INFO_MESSAGES = 1;
   }
   if( d >= 2 ) {
      // info and debug 
      _SG_DEBUG_MESSAGES = 1;
   }
}

void md_set_error_level( int e ) {
   if( e <= 0 ) {
      // no error 
      _SG_ERROR_MESSAGES = 0;
      _SG_WARN_MESSAGES = 0;
   }
   if( e >= 1 ) {
      _SG_ERROR_MESSAGES = 1;
   }
   if( e >= 2 ) {
      _SG_WARN_MESSAGES = 1;
   }
}

int md_get_debug_level() {
   return _SG_DEBUG_MESSAGES;
}

int md_get_error_level() {
   return _SG_ERROR_MESSAGES;
}

/* Converts a hex character to its integer value */
static char from_hex(char ch) {
  return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10;
}

/* Converts an integer value to its hex character*/
static char to_hex(char code) {
  static char hex[] = "0123456789abcdef";
  return hex[code & 15];
}


/*
 * Get the current time in seconds since the epoch, local time
 */
int64_t md_current_time_seconds() {
   struct timespec ts;
   int rc = clock_gettime( CLOCK_REALTIME, &ts );
   if( rc != 0 ) {
      return -errno;
   }
   
   int64_t ts_sec = (int64_t)ts.tv_sec;
   return ts_sec;
}


// get the current time in milliseconds
int64_t md_current_time_millis() {
   struct timespec ts;
   int rc = clock_gettime( CLOCK_REALTIME, &ts );
   if( rc != 0 ) {
      return -errno;
   }
   
   int64_t ts_sec = (int64_t)ts.tv_sec;
   int64_t ts_nsec = (int64_t)ts.tv_nsec;
   return (ts_sec * 1000) + (ts_nsec / 1000000);
}

// difference in time, in milliseconds
// find t1 - t2
int64_t md_timespec_diff_ms( struct timespec* t1, struct timespec* t2 ) {
  
   int64_t sec = t1->tv_sec;
   int64_t nsec = t1->tv_nsec;

   if( t2->tv_nsec > nsec ) {
      
      sec--;
      nsec += 1000000000L;
   }
   
   sec -= t2->tv_sec;
   nsec -= t2->tv_nsec;
   
   return (sec * 1000) + (nsec / 1000000);
}

// difference in time
// find t1 - t2
int64_t md_timespec_diff( struct timespec* t1, struct timespec* t2 ) {
   
   int64_t sec = t1->tv_sec;
   int64_t nsec = t1->tv_nsec;

   if( t2->tv_nsec > nsec ) {
      
      sec--;
      nsec += 1000000000L;
   }
   
   sec -= t2->tv_sec;
   nsec -= t2->tv_nsec;
   
   return (sec * 1000000000L) + (nsec);
}


/*
 * Get the user's umask
 */
mode_t md_get_umask() {
  mode_t mask = umask(0);
  umask(mask);
  return mask;
}<commit_msg>Clearer calculation of a millisecond timespec diff<commit_after>/*
   Copyright 2013 The Trustees of Princeton University

   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.
*/
/*
 * Utility functions (debugging, etc)
 */
 
#include "libsyndicate/util.h"
#include "libsyndicate/libsyndicate.h"

int _SG_DEBUG_MESSAGES = 0;
int _SG_INFO_MESSAGES = 0;

// log all warnings and errors by default
int _SG_WARN_MESSAGES = 1;
int _SG_ERROR_MESSAGES = 1;


void md_set_debug_level( int d ) {
   if( d <= 0 ) {
      // no debugging
      _SG_DEBUG_MESSAGES = 0;
      _SG_INFO_MESSAGES = 0;
   }
   if( d >= 1 ) {
      _SG_INFO_MESSAGES = 1;
   }
   if( d >= 2 ) {
      // info and debug 
      _SG_DEBUG_MESSAGES = 1;
   }
}

void md_set_error_level( int e ) {
   if( e <= 0 ) {
      // no error 
      _SG_ERROR_MESSAGES = 0;
      _SG_WARN_MESSAGES = 0;
   }
   if( e >= 1 ) {
      _SG_ERROR_MESSAGES = 1;
   }
   if( e >= 2 ) {
      _SG_WARN_MESSAGES = 1;
   }
}

int md_get_debug_level() {
   return _SG_DEBUG_MESSAGES;
}

int md_get_error_level() {
   return _SG_ERROR_MESSAGES;
}

/* Converts a hex character to its integer value */
static char from_hex(char ch) {
  return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10;
}

/* Converts an integer value to its hex character*/
static char to_hex(char code) {
  static char hex[] = "0123456789abcdef";
  return hex[code & 15];
}


/*
 * Get the current time in seconds since the epoch, local time
 */
int64_t md_current_time_seconds() {
   struct timespec ts;
   int rc = clock_gettime( CLOCK_REALTIME, &ts );
   if( rc != 0 ) {
      return -errno;
   }
   
   int64_t ts_sec = (int64_t)ts.tv_sec;
   return ts_sec;
}


// get the current time in milliseconds
int64_t md_current_time_millis() {
   struct timespec ts;
   int rc = clock_gettime( CLOCK_REALTIME, &ts );
   if( rc != 0 ) {
      return -errno;
   }
   
   int64_t ts_sec = (int64_t)ts.tv_sec;
   int64_t ts_nsec = (int64_t)ts.tv_nsec;
   return (ts_sec * 1000) + (ts_nsec / 1000000);
}

// difference in time, in milliseconds
// find t1 - t2
int64_t md_timespec_diff_ms( struct timespec* t1, struct timespec* t2 ) {
  
   int64_t sec = t1->tv_sec;
   int64_t nsec = t1->tv_nsec;

   if( t2->tv_nsec > nsec ) {
      
      sec--;
      nsec += 1000000000L;
   }

   return ((sec - t2->tv_sec) * 1000) + ((nsec - t2->tv_nsec)/1000000);
}

// difference in time
// find t1 - t2
int64_t md_timespec_diff( struct timespec* t1, struct timespec* t2 ) {
   
   int64_t sec = t1->tv_sec;
   int64_t nsec = t1->tv_nsec;

   if( t2->tv_nsec > nsec ) {
      
      sec--;
      nsec += 1000000000L;
   }
   
   sec -= t2->tv_sec;
   nsec -= t2->tv_nsec;
   
   return (sec * 1000000000L) + (nsec);
}


/*
 * Get the user's umask
 */
mode_t md_get_umask() {
  mode_t mask = umask(0);
  umask(mask);
  return mask;
}
{'fraction_non_alphanumeric': 0.11002260738507913, 'fraction_numerical': 0.0428033157498116, 'mean_word_length': 2.662251655629139, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 6, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '620702', 'n_tokens_mistral': 2724, 'n_tokens_neox': 2463, 'n_words': 711}