EAIIG HD
㈱東アジア国際産業グループ
ホールディングス
コラム
ソフトウェア技術公開
( 社外向けページ )
( From Company to Outer )
( From Company to Others )
EAIIG HD
㈱東アジア国際産業グループ
ホールディングス
( 社外向けページ )
( From Company to Outer )
( From Company to Others )
EAIIG HD >コラム:ソフトウェア技術公開
!催奇性薬品は絶対禁止!
10:53 2022/02/21 子供(コドモ:Codomo)=Child, Children である。
2021/08/22 08:53:00
批判があり閉鎖します。御了承ください。取得はお早目に!
プロセスコリジョンせずに記録されます。悪しからず御了承ください。
using System;
using System.Text;
using System.Windows.Forms;
//add
using System.IO;
using System.Security.AccessControl;
using System.Diagnostics;
// add
using System.Threading;
using System.Security.Permissions;
using System.Security;
namespace PeacePadEditor01
{
class PPad1_Log01Core
{
private static FileInfo finf = null;
private const string LOG_FILE_NAME = ".PeacePad.log";
private readonly string file = Application.LocalUserAppDataPath + "\\" + LOG_FILE_NAME;
private readonly string init_log_str = "init_dfjreouitguhlyla";
private const string _MUTEX_NAME = "ppad1_ifjsjludsrhfgks";
private const Int32 _MUTEX_time_out = 60000;
private static Mutex Mut = null;
private const string _CHK_LOG_COMMIT_STR = "_COMMIT";
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// PPad1_Log01 コンストラクタ
/// </summary>
protected PPad1_Log01Core()
{
//this._trace( "PPad1_Log01Core() start" );
try
{
Mut = Mutex.OpenExisting( _MUTEX_NAME );
}
catch ( WaitHandleCannotBeOpenedException )
{
Mut = new Mutex( false, _MUTEX_NAME );
}
//this._trace( "PPad1_Log01Core() end" );
// v0.98.0052.02, v0.98.0052.03
FileIOPermission f2 = new FileIOPermission( FileIOPermissionAccess.Write, this.file );
try
{
f2.Demand();
}
catch ( SecurityException ex )
{
CS_MsgBox.warningShow( ex.ToString() );
}
}
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// checkLogFile()
/// </summary>
public virtual void checkLogFile()
{
// this._trace( "checkLogFile() start" );
try
{
Mut.WaitOne( _MUTEX_time_out );
{
this.checkLogFileCore();
}
Mut.ReleaseMutex();
}
catch ( Exception ex )
{
Mut.ReleaseMutex();
this.outLog05( ex.ToString() );
CS_MsgBox.warningShow( ex.ToString() );
}
// this._trace( "checkLogFile() end" );
}
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// checkLogFileCore()
/// </summary>
private void checkLogFileCore()
{
PPad1_Message pmd = PPad1_Message.Default;
Properties.Settings psd = Properties.Settings.Default;
if ( !File.Exists( this.file ) )
{
File.AppendAllText( this.file, "", Encoding.UTF8 );
this.outLog07( pmd.MES_WARN_MAKE_LOG );
CS_MsgBox.warningShow( pmd.MES_WARN_MAKE_LOG );
if ( ( 0 == psd.chk_log_hash ) &&
( psd.chk_log_commit_str.Equals( this.init_log_str ) ) )
{
// ログ
this.outLog07( pmd.MES_WARN_START_LOG );
CS_MsgBox.warningShow( pmd.MES_WARN_START_LOG );
}
else
{
this.outLog07( pmd.MES_WARN_DELETE_LOG );
CS_MsgBox.warningShow( pmd.MES_WARN_DELETE_LOG );
}
}
else
{
// check
int log_hash = this.getLogChkDat( psd.chk_log_commit_str );
int chk_log_hash = psd.chk_log_hash;
if ( log_hash != chk_log_hash )
{
// 改ざんか? ログファイルをデスクトップに。
//
string user_desktop_path
= Environment.GetFolderPath( Environment.SpecialFolder.Desktop );
string fmt3 = Properties.Settings.Default.FORMAT_DATA_TIME3;
string add_name = "_" + DateTime.Now.ToString( fmt3 );
string file2 = user_desktop_path + "\\"
+ Path.GetFileNameWithoutExtension( this.file )
+ add_name
+ Path.GetExtension( this.file );
//
File.Copy( this.file, file2, true );
this.outLog07( pmd.MES_WARN_COPY_LOG );
CS_MsgBox.warningShow( pmd.MES_WARN_COPY_LOG );
}
}
}
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// regLogChkDat() v0.98.0047.24
/// </summary>
public virtual void regLogChkDat()
{
Mut.WaitOne( _MUTEX_time_out );
{
string chk_log_commit_str = this.getLogChkCommitStr();
if ( null == finf )
{
finf = new FileInfo( this.file );
}
finf.Refresh();
string log = "size: " + finf.Length.ToString() + " byte, ";
this.outLog07( log + chk_log_commit_str );
{
int log_hash = this.getLogChkDat( chk_log_commit_str );
//
Properties.Settings psd = Properties.Settings.Default;
psd.chk_log_hash = log_hash;
psd.chk_log_commit_str = chk_log_commit_str;
//psd.chk_log_hash = 0; //
//psd.chk_log_commit_str =this.init_log_str; //
psd.Save();
}
}
Mut.ReleaseMutex();
}
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Hellper:etLogChkCommitStr()
/// </summary>
/// <returns></returns>
private string getLogChkCommitStr()
{
string fmt2 = Properties.Settings.Default.FORMAT_DATA_TIME2;
string str = _CHK_LOG_COMMIT_STR + "_" + DateTime.Now.ToString( fmt2 );
return str;
}
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Hellper:getLogChkDat()
/// </summary>
/// <returns></returns>
private int getLogChkDat( string str_commit )
{
try
{
string text = null;
text = File.ReadAllText( this.file, Encoding.UTF8 );
int idx = text.IndexOf( str_commit );
if( -1 == idx ) return 0;
int log_hash = text.Substring( 0, idx ).GetHashCode();
return log_hash;
}
catch ( Exception ex )
{
this.outLog07( ex.ToString() );
return 0;
}
}
/////////////////////////////////////////////////////////////////////////
/// <summary>
/// デストラクタ、Mutexの破棄
/// </summary>
~PPad1_Log01Core()
{
if ( null != Mut )
{
Mut.Close();
}
}
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// ログ出力 v0.98.0047.23
/// </summary>
/// <param name="str_log"></param>
/// <returns></returns>
public virtual bool outLog05( string str_log )
{
try
{
if ( ( null == str_log ) || ( str_log.Equals( "" ) ) ) return false;
Mut.WaitOne( _MUTEX_time_out );
{
// 日付と時刻などを加える。
string text = this.getLogStr( str_log );
// ファイルの作成
if ( !File.Exists( this.file ) )
{
File.AppendAllText( this.file, "", Encoding.UTF8 );
}
if ( null == finf )
{
finf = new FileInfo( this.file );
}
finf.Refresh();
if ( 1024 * 1024 < finf.Length )
{
this.copyLogFileWhenOverSize();
//
string text2 = File.ReadAllText( this.file, Encoding.UTF8 );
text2 = text2.Substring( text2.Length / 2 );
this.modfyFileSecurity( this.file );
// v0.98.0065
File.Delete( this.file );
// File.Create( this.file );
// File.Move( this.file, "x" + this.file); // .Delete( this.file );
// FileInfo fi1 = new FileInfo( this.file );
// fi1.Delete();
// File.AppendAllText( this.file, text2, Encoding.UTF8 );
File.WriteAllText( this.file, text2, Encoding.UTF8 );
//
this.regLogChkDat();
}
else
{
this.modfyFileSecurity( this.file );
File.AppendAllText( this.file, text, Encoding.UTF8 );
this.resetFileSecurity( this.file );
}
}
Mut.ReleaseMutex();
}
catch ( Exception ex )
{
Mut.ReleaseMutex();
CS_MsgBox.warningShow( ex.ToString() );
return false;
}
return true;
}
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// ログ出力 v0.98.0047.23 (Simple & No Mutex)
/// </summary>
/// <param name="str_log"></param>
/// <returns></returns>
private bool outLog07( string str_log )
{
try
{
if ( ( null == str_log ) || ( str_log.Equals( "" ) ) ) return false;
// 日付と時刻などを加える。
string text = this.getLogStr( str_log );
// ファイルの作成
if ( !File.Exists( this.file ) )
{
File.AppendAllText( this.file, "", Encoding.UTF8 );
}
this.modfyFileSecurity( this.file );
File.AppendAllText( this.file, text, Encoding.UTF8 );
this.resetFileSecurity( this.file );
}
catch ( Exception ex )
{
CS_MsgBox.warningShow( ex.ToString() );
return false;
}
return true;
}
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Helper:modfyFileSecurity()
/// </summary>
/// <param name="str_fname"></param>
private void modfyFileSecurity( string str_fname )
{
this.AddFileSecurity3( str_fname );
File.SetAttributes( str_fname,
File.GetAttributes( str_fname ) & ( ~FileAttributes.ReadOnly ) );
}
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Helper:resetFileSecurity()
/// </summary>
/// <param name="str_fname"></param>
private void resetFileSecurity( string str_fname )
{
File.SetAttributes( str_fname,
File.GetAttributes( str_fname ) | FileAttributes.ReadOnly | FileAttributes.Hidden );
this.RemoveFileSecurity2( str_fname );
}
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Helper:getLogStr();
/// </summary>
/// <param name="str_log"></param>
/// <returns></returns>
private string getLogStr( string str_log )
{
string fmt2 = Properties.Settings.Default.FORMAT_DATA_TIME2;
return DateTime.Now.ToString( fmt2 ) + ", "
+ Environment.UserName + ", "
+ str_log + Environment.NewLine;
}
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// ファイルACLエントリ追加2
/// Adds an ACL entry on the specified file for the specified account.
/// </summary>
/// <param name="file_name"></param>
/// <param name="rights"></param>
/// <param name="ac_type"></param>
private void AddFileSecurity2( string file_name )
{
FileSecurity fs = File.GetAccessControl( file_name );
fs.SetAccessRuleProtection( true, false );
fs.RemoveAccessRule( this.newAccessRuleWriteDeny() );
fs.RemoveAccessRule( this.newAccessRuleModifyDeny() );
fs.AddAccessRule( this.newAccessRuleWrite() );
File.SetAccessControl( file_name, fs );
}
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// ファイルACLエントリ追加3
/// Adds an ACL entry on the specified file for the specified account.
/// </summary>
/// <param name="file_name"></param>
/// <param name="rights"></param>
/// <param name="ac_type"></param>
private void AddFileSecurity3( string file_name )
{
FileSecurity fs = File.GetAccessControl( file_name );
fs.SetAccessRuleProtection( true, false );
fs.RemoveAccessRule( this.newAccessRuleWriteDeny() );
fs.RemoveAccessRule( this.newAccessRuleModifyDeny() );
fs.AddAccessRule( this.newAccessRuleWrite() );
fs.AddAccessRule( this.newAccessRuleModify() );
File.SetAccessControl( file_name, fs );
}
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// ファイルACLエントリ削除2
/// Removes an ACL entry on the specified file for the specified account.
/// </summary>
/// <param name="file_name"></param>
/// <param name="rights"></param>
/// <param name="ac_type"></param>
private void RemoveFileSecurity2( string file_name )
{
FileSecurity fs = File.GetAccessControl( file_name );
fs.RemoveAccessRule( this.newAccessRuleWrite() );
fs.AddAccessRule( this.newAccessRuleWriteDeny() );
fs.AddAccessRule( this.newAccessRuleRead() );
File.SetAccessControl( file_name, fs );
}
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// newAccessRuleWrite
/// </summary>
/// <returns></returns>
private FileSystemAccessRule newAccessRuleWrite()
{
return new FileSystemAccessRule(
Environment.UserName,
FileSystemRights.Write, InheritanceFlags.None, PropagationFlags.None,
AccessControlType.Allow );
}
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// newAccessRuleModify // v0.98.0065
/// </summary>
/// <returns></returns>
private FileSystemAccessRule newAccessRuleModify()
{
return new FileSystemAccessRule(
Environment.UserName,
FileSystemRights.Modify, InheritanceFlags.None, PropagationFlags.None,
AccessControlType.Allow );
}
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// newAccessRuleRead
/// </summary>
/// <returns></returns>
private FileSystemAccessRule newAccessRuleRead()
{
return new FileSystemAccessRule(
Environment.UserName,
FileSystemRights.Read, InheritanceFlags.None, PropagationFlags.None,
AccessControlType.Allow );
}
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Helper:newAccessRuleWriteDeny
/// </summary>
/// <returns></returns>
private FileSystemAccessRule newAccessRuleWriteDeny()
{
return new FileSystemAccessRule(
Environment.UserName,
FileSystemRights.Write,
AccessControlType.Deny );
}
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Helper:newAccessRuleModifyDeny
/// </summary>
/// <returns></returns>
private FileSystemAccessRule newAccessRuleModifyDeny()
{
return new FileSystemAccessRule(
Environment.UserName,
FileSystemRights.Modify,
AccessControlType.Deny );
}
// ----------------------------------------------------------------------------------
private readonly string log_folder
= Environment.GetFolderPath( Environment.SpecialFolder.MyDocuments )
+ "\\" + "PeacePad_Log";
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// copyLogFileWhenOverSize()
/// </summary>
private void copyLogFileWhenOverSize()
{
//
this.makeLogFolder();
try
{
Mut.WaitOne( _MUTEX_time_out );
{
string local_path = this.getLocalPath();
string fmt3 = Properties.Settings.Default.FORMAT_DATA_TIME3;
string add_name = "_" + DateTime.Now.ToString( fmt3 );
string local_path2
= Path.GetDirectoryName( local_path ) + "\\"
+ Path.GetFileNameWithoutExtension( local_path )
+ add_name
+ Path.GetExtension( local_path );
File.Copy( this.file, local_path2, true );
}
Mut.ReleaseMutex();
}
catch ( Exception ex )
{
Mut.ReleaseMutex();
this.outLog05( ex.ToString() );
CS_MsgBox.warningShow( ex.ToString() );
}
}
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// makeLogFolder()
/// </summary>
private void makeLogFolder()
{
try
{
Mut.WaitOne( _MUTEX_time_out );
{
if ( !Directory.Exists( this.log_folder ) )
{
Directory.CreateDirectory( this.log_folder );
string mes = PPad1_Message.Default.MES_INF_MAKE_LOG_FOLDER;
this.outLog07( mes );
CS_MsgBox.warningShow( mes );
}
}
Mut.ReleaseMutex();
}
catch ( Exception ex )
{
Mut.ReleaseMutex();
this.outLog05( ex.ToString() );
}
}
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// copyLogFileToMyDoc()
/// </summary>
private void copyLogFileToMyDoc()
{
//
this.makeLogFolder();
try
{
Mut.WaitOne( _MUTEX_time_out );
{
string local_path = this.getLocalPath();
if ( !File.Exists( local_path ) )
{
File.Copy( this.file, local_path, true );
}
else
{
this.modfyFileSecurity( local_path );
File.Copy( this.file, local_path, true );
this.resetFileSecurity( local_path );
}
}
Mut.ReleaseMutex();
}
catch ( Exception ex )
{
Mut.ReleaseMutex();
this.outLog05( ex.ToString() );
CS_MsgBox.warningShow( ex.ToString() );
}
}
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Helper:getLocalPath()
/// </summary>
/// <returns></returns>
private string getLocalPath()
{
return this.log_folder + "\\" + Path.GetFileName( this.file );
}
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// openLogFile()
/// </summary>
public virtual void openLogFile()
{
string local_path = this.getLocalPath();
//
this.copyLogFileToMyDoc();
// ダブルコーテーション(エスケープ文字)
string str_double_q = "\"";
// (アバスト併用だと、かなり遅い。設定>アンチウィルス>ディープスクリーンのチェックをOFFにすると起動が速くなる)
Process.Start( Application.ExecutablePath.ToString(),
str_double_q + local_path + str_double_q );
}
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Debug:_trace()
/// </summary>
/// <param name="str_log"></param>
public virtual void _trace( string str_log )
{
this.outLog07( str_log );
}
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// ログファイル名 v0.98.0090.02
/// </summary>
/// <returns></returns>
public virtual string getLogFileName()
{
return LOG_FILE_NAME;
}
} // PPad1_Log01
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// interface: IPPad1_Log01
/// </summary>
public interface IPPad1_Log01
{
void checkLogFile();
void regLogChkDat();
bool outLog05( string str_log );
void openLogFile();
string getLogFileName(); // v0.98.0090.02
}
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// class PPad1_Log01
/// </summary>
sealed class PPad1_Log01 : PPad1_Log01Core, IPPad1_Log01
{
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// checkLogFile()
/// </summary>
public override void checkLogFile()
{
base.checkLogFile();
}
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// regLogChkDat()
/// </summary>
public override void regLogChkDat()
{
base.regLogChkDat();
}
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// outLog05()
/// </summary>
/// <param name="str_log"></param>
/// <returns></returns>
public override bool outLog05( string str_log )
{
return base.outLog05( str_log );
}
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// openLogFile()
/// </summary>
public override void openLogFile()
{
base.openLogFile();
}
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// getLogFileName() v0.98.0090.02
/// </summary>
/// <returns></returns>
public override string getLogFileName()
{
return base.getLogFileName();
}
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Debug: _trace()
/// </summary>
/// <param name="str_log"></param>
public override void _trace( string str_log )
{
base._trace( str_log );
}
}
}