<commit_before>/***************************************************************
 *
 * Copyright (C) 1990-2007, Condor Team, Computer Sciences Department,
 * University of Wisconsin-Madison, WI.
 *
 * 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.
 *
 ***************************************************************/

#include "condor_common.h"
#include "condor_debug.h"
#include "util_lib_proto.h"
#include <stdarg.h>
#include "write_user_log.h"
#include "read_user_log.h"
#include <time.h>
#include "condor_uid.h"
#include "condor_xml_classads.h"
#include "condor_config.h"
#include "utc_time.h"
#include "stat_wrapper.h"
#include "file_lock.h"
#include "user_log_header.h"

// Set to non-zero to enable fine-grained rotation debugging / timing
#define ROTATION_TRACE	0

static const char SynchDelimiter[] = "...\n";

// Simple class to normalize use of 64 bit ints
class UserLogInt64_t
{
public:
	UserLogInt64_t( void )
		{ m_value = 0; };
	UserLogInt64_t( int64_t value )
		{ m_value = value; };
	~UserLogInt64_t( void );

	int64_t Set( int64_t value )
		{ return m_value = value; };
	int64_t Get( void ) const
		{ return m_value; };
	UserLogInt64_t& operator =( int64_t value )
		{ m_value = value; return *this; };
	UserLogInt64_t& operator +=( int64_t value )
		{ m_value += value; return *this; };

private:
	int64_t		m_value;

};
class UserLogFilesize_t : public UserLogInt64_t
{
};


// ***************************
//   UserLog constructors
// ***************************
UserLog::UserLog( void )
{
	Reset( );
}

UserLog::UserLog (const char *owner,
                  const char *domain,
                  const char *file,
                  int c,
                  int p,
                  int s,
                  bool xml, const char *gjid)
{
	Reset();
	m_use_xml = xml;

	initialize (owner, domain, file, c, p, s, gjid);
}

/* This constructor is just like the constructor above, except
 * that it doesn't take a domain, and it passes NULL for the domain and
 * the globaljobid. Hopefully it's not called anywhere by the condor code...
 * It's a convenience function, requested by our friends in LCG. */
UserLog::UserLog (const char *owner,
                  const char *file,
                  int c,
                  int p,
                  int s,
                  bool xml)
{
	Reset( );
	m_use_xml = xml;

	initialize (owner, NULL, file, c, p, s, NULL);
}

// Destructor
UserLog::~UserLog()
{
	FreeResources( );
}


// ********************************
//   UserLog initialize() methods
// ********************************
bool
UserLog::initialize( const char *file, int c, int p, int s, const char *gjid)
{
		// Save parameter info
	m_path = new char[ strlen(file) + 1 ];
	strcpy( m_path, file );

	if( m_fp ) {
		if( fclose( m_fp ) != 0 ) {
			dprintf( D_ALWAYS, "UserLog::initialize: "
					 "fclose(\"%s\") failed - errno %d (%s)\n", m_path,
					 errno, strerror(errno) );
		}
		m_fp = NULL;
	}

	if ( m_userlog_enable &&
		 !openFile(file, true, m_enable_locking, true, m_lock, m_fp) ) {
		dprintf(D_ALWAYS, "UserLog::initialize: failed to open file\n");
		return false;
	}

	return initialize(c, p, s, gjid);
}

bool
UserLog::initialize( const char *owner, const char *domain, const char *file,
					 int c, int p, int s, const char *gjid )
{
	priv_state		priv;

	uninit_user_ids();
	if (!  init_user_ids(owner, domain) ) {
		dprintf(D_ALWAYS, "UserLog::initialize: init_user_ids() failed!\n");
		return false;
	}

		// switch to user priv, saving the current user
	priv = set_user_priv();

		// initialize log file
	bool res = initialize(file, c, p, s, gjid);

		// get back to whatever UID and GID we started with
	set_priv(priv);

	return res;
}

bool
UserLog::initialize( int c, int p, int s, const char *gjid )
{
	m_cluster = c;
	m_proc = p;
	m_subproc = s;

	Configure( );

		// Important for performance : note we do not re-open the global log
		// if we already have done so (i.e. if m_global_fp is not NULL).
	if ( m_global_enable && !m_global_fp ) {
		priv_state priv = set_condor_priv();
		UserLogHeader	header;
		initializeGlobalLog( header );
		set_priv( priv );
	}

	if(gjid) {
		m_gjid = strdup(gjid);
	}

	return true;
}

// Read in our configuration information
bool
UserLog::Configure( void )
{
	m_enable_fsync = param_boolean( "ENABLE_USERLOG_FSYNC", true );
	m_enable_locking = param_boolean( "ENABLE_USERLOG_LOCKING", true );

	m_global_path = param( "EVENT_LOG" );
	if ( NULL == m_global_path ) {
		return true;<commit_msg>Ooops, this should have been a part of commit <PASSWORD> (#451)<commit_after>/***************************************************************
 *
 * Copyright (C) 1990-2007, Condor Team, Computer Sciences Department,
 * University of Wisconsin-Madison, WI.
 *
 * 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.
 *
 ***************************************************************/

#include "condor_common.h"
#include "condor_debug.h"
#include "util_lib_proto.h"
#include <stdarg.h>
#include "write_user_log.h"
#include "read_user_log.h"
#include <time.h>
#include "condor_uid.h"
#include "condor_xml_classads.h"
#include "condor_config.h"
#include "utc_time.h"
#include "stat_wrapper.h"
#include "file_lock.h"
#include "user_log_header.h"

// Set to non-zero to enable fine-grained rotation debugging / timing
#define ROTATION_TRACE	0

static const char SynchDelimiter[] = "...\n";

// Simple class to normalize use of 64 bit ints
class UserLogInt64_t
{
public:
	UserLogInt64_t( void )
		{ m_value = 0; };
	UserLogInt64_t( int64_t value )
		{ m_value = value; };
	~UserLogInt64_t( void );

	int64_t Set( int64_t value )
		{ return m_value = value; };
	int64_t Get( void ) const
		{ return m_value; };
	UserLogInt64_t& operator =( int64_t value )
		{ m_value = value; return *this; };
	UserLogInt64_t& operator +=( int64_t value )
		{ m_value += value; return *this; };

private:
	int64_t		m_value;

};
class UserLogFilesize_t : public UserLogInt64_t
{
};


// ***************************
//   UserLog constructors
// ***************************
UserLog::UserLog( void )
{
	Reset( );
}

UserLog::UserLog (const char *owner,
                  const char *domain,
                  const char *file,
                  int c,
                  int p,
                  int s,
                  bool xml, const char *gjid)
{
	Reset();
	m_use_xml = xml;

	initialize (owner, domain, file, c, p, s, gjid);
}

/* This constructor is just like the constructor above, except
 * that it doesn't take a domain, and it passes NULL for the domain and
 * the globaljobid. Hopefully it's not called anywhere by the condor code...
 * It's a convenience function, requested by our friends in LCG. */
UserLog::UserLog (const char *owner,
                  const char *file,
                  int c,
                  int p,
                  int s,
                  bool xml)
{
	Reset( );
	m_use_xml = xml;

	initialize (owner, NULL, file, c, p, s, NULL);
}

// Destructor
UserLog::~UserLog()
{
	FreeResources( );
}


// ********************************
//   UserLog initialize() methods
// ********************************
bool
UserLog::initialize( const char *file, int c, int p, int s, const char *gjid)
{
		// Save parameter info
	m_path = new char[ strlen(file) + 1 ];
	strcpy( m_path, file );

	if( m_fp ) {
		if( fclose( m_fp ) != 0 ) {
			dprintf( D_ALWAYS, "UserLog::initialize: "
					 "fclose(\"%s\") failed - errno %d (%s)\n", m_path,
					 errno, strerror(errno) );
		}
		m_fp = NULL;
	}

	if ( m_userlog_enable &&
		 !openFile(file, true, m_enable_locking, true, m_lock, m_fp) ) {
		dprintf(D_ALWAYS, "UserLog::initialize: failed to open file\n");
		return false;
	}

	return initialize(c, p, s, gjid);
}

bool
UserLog::initialize( const char *owner, const char *domain, const char *file,
					 int c, int p, int s, const char *gjid )
{
	priv_state		priv;

	uninit_user_ids();
	if (!  init_user_ids(owner, domain) ) {
		dprintf(D_ALWAYS, "UserLog::initialize: init_user_ids() failed!\n");
		return false;
	}

		// switch to user priv, saving the current user
	priv = set_user_priv();

		// initialize log file
	bool res = initialize(file, c, p, s, gjid);

		// get back to whatever UID and GID we started with
	set_priv(priv);

	return res;
}

bool
UserLog::initialize( int c, int p, int s, const char *gjid )
{
	m_cluster = c;
	m_proc = p;
	m_subproc = s;

	Configure( );

		// Important for performance : note we do not re-open the global log
		// if we already have done so (i.e. if m_global_fp is not NULL).
	if ( m_global_enable && !m_global_fp ) {
		priv_state priv = set_condor_priv();
		UserLogHeader	header;
		initializeGlobalLog( header );
		set_priv( priv );
	}

	if(gjid) {
		m_gjid = strdup(gjid);
	}

	return true;
}

// Read in our configuration information
bool
UserLog::Configure( void )
{
	FreeResources( );

	m_enable_fsync = param_boolean( "ENABLE_USERLOG_FSYNC", true );
	m_enable_locking = param_boolean( "ENABLE_USERLOG_LOCKING", true );

	m_global_path = param( "EVENT_LOG" );
	if ( NULL == m_global_path ) {
		return true;
{'fraction_non_alphanumeric': 0.16094591897824786, 'fraction_numerical': 0.009479145879066054, 'mean_word_length': 3.3787680209698556, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 14}, 'pii_count': 0, 'substrings_counts': 0, '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': '1189247', 'n_tokens_mistral': 3561, 'n_tokens_neox': 3312, 'n_words': 1050}