2019년 1월 22일 화요일

UTF-8 문자열 바이트 기준으로 자르기.

어쩌다 해야 할 일이 있는데 기억력이 좋지 못해서 ...


 1. UTF-8


  • 0xxxxxxx - ASCII와 동일
  • 11(0-1)xxxxx 10xxxxxx (10xxxxxx) - 2, 3 바이트 문자의 경우 첫 번째 바이트는 110 또는 1110으로 시작
  • 4 바이트 표현도 존재하지만 자르는데 의미는 없으므로 생략.


2. 예제.

 
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import re
 
def cut_utf8_by_byte(text, length):
    """
    """
 
    if len(text) >= length:
        if re.match('[\xE0-\xFF]', text[length]) is not None:
            length -= 1
        elif re.match('[\x80-\xFF]', text[length]) is not None:
            while True:
                length -= 1
                if re.match('[\xDF-\xFF]', text[length]) is not None:
                    break
    return text[:length]
cs

3. 결과.


  
.

2018년 2월 14일 수요일

시간 정보가 포함된 history

기억력이 가물 가물해져서 마지막 빌드를 오늘 했는지 어제 했는지 ...


1. history

   /etc/profile file 마지막에 다음 2 줄을 추가.

   
1
2
HISTTIMEFORMAT="%F %T -- "
export HISTTIMEFORMAT
cs


   그리고 반영한다.

   
1
source /etc/profile
cs

2. 결과


 

2018년 2월 13일 화요일

CentOS 6.x 버전에 PostgreSQL 최신 버전 설치하기 - #02.

yum install postgresql-server 까지는 동일. 하지만,


1. Post-installation commands.

   공식 문서와 마찬가지로 이번엔 /var/lib/pgsql/9.3/data/ 경로로 directory가 
   생성되었습니다. 그리고 마찬가지로 초기화도 버전 정보가 포함된 상태로 
   진행합니다.

   
1
service postgresql-9.3 initdb
cs


2. Startup

   그리고 서비스를 실행합니다.

   
1
2
chkconfig postgresql-9.3 on
service postgresql-9.3 start
cs


   이 후 진행은 CentOS 7.0에서 PostgreSQL 외부 접속 허용하기 대로 진행한다.

2018년 2월 12일 월요일

CentOS 6.x 버전에 PostgreSQL 최신 버전 설치하기 - #01.

서비스 환경이 그러하다.


1. CentOS 6.5

   제목은 6.x라고 정했지만 우리는 6.5를 사용중이기 때문에 6.5를 사용합니다.


   보시다시피 8.4.x 버전 이외에 다른 버전은 없습니다.

2. 저장소 변경.

   최신 버전을 설치할 수 있도록 저장소를 변경합니다.

1
vi /etc/yum.repos.d/CentOS-Base.repo
cs

   그리고, [base]와 [updates] 항목에 postgresql 관련 항목을 무시하도록 추가합니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[base]
name=CentOS-$releasever - Base
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
#baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
exclude=postgresql*
#released updates
[updates]
name=CentOS-$releasever - Updates
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
#baseurl=http://mirror.centos.org/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
exclude=postgresql*
cs


3. 저장소 설치.

  • 다운로드 후 설치
    • 1
      2
      curl -O http://yum.postgresql.org/9.3/redhat/rhel-6-x86_64/pgdg-centos93-9.3-3.noarch.rpm
      rpm -ivh pgdg-centos93-9.3-3.noarch.rpm
      cs
  • 바로 설치
    • 1
      rpm -Uvh  https://download.postgresql.org/pub/repos/yum/9.3/redhat/rhel-6-x86_64/pgdg-centos93-9.3-3.noarch.rpm
      cs

4. PostgreSQL 설치.

   그리고 그 다음은 그냥 설치하면 된다.

2018년 1월 17일 수요일

PyCrypto 64 비트 윈도우에 설치하기.

새빨간 글씨로 설치가 안된다고 투덜거린다.


1. pip install ./pgadmin4-2.1-py2.py3-none-any.whl


   Failed building wheel for pycrypto

   ...
 

2. The Voidspace Python Modules


   고맙게도 만들어 놓은 훌륭한 분이 존재하고 설치만 하면 된다.
   wheel 바이너리를 다운로드 하면 .zip 확장자를 갖는데 .whl로 변경 후 설치 진행.

  
1
pip install ./pycrypto-2.6.1-cp27-none-win_amd64.whl
cs

2017년 12월 1일 금요일

GDB - Missing separate debuginfos, use: debuginfo-install ...

어라 디버깅이 안 되네.


Missing separate debuginfos, use: debuginfo-install glibc-2.17-55.el7_0.3.x86_64 libgcc-4.8.2-16.2.el7_0.x86_64 libstdc++-4.8.2-16.2.el7_0.x86_64
이런게 gdb 실행 중에 보인다면.

1. debuginfos.


1
2
yum install yum-utils
debuginfo-install glibc libgcc libstdc++
cs


   그럼 문제가 없어진다.


2. 하지만 그래도 안되는 경우엔


   적절한 에디터로 다음 파일을 열어서 /etc/yum.repos.d/CentOS-Debuginfo.repo
   그 중 enabled 값을 1로 변경해 보라고 한다.

  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# CentOS-Debug.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client.  You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
 
# All debug packages from all the various CentOS-7 releases
# are merged into a single repo, split by BaseArch
#
# Note: packages in the debuginfo repo are currently not signed
#
 
[base-debuginfo]
name=CentOS-7 - Debuginfo
baseurl=http://debuginfo.centos.org/7/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-Debug-7
enabled=1
#
cs

2017년 11월 2일 목요일

Dr. Memory

메모리가 새는 곳을 찾아라.


C/C++ 개발을 하다가 보면 동반자와 같은 memory leak ...


1. Valgrind.


   좋은 툴이지만 10년 넘게 사용하다 보니 지겹다.
   좋은 툴이 아니라서 사용 안한게 아니니 오해하기 없기.


2. Dr. Memory.


   새로운 툴을 찾아 열심히 돌아다니던 중 valgrind처럼 오픈 소스인데 윈도우, 리눅스,
   맥, 그리고 안드로이드까지 지원한단다. 거기에다 맙소사 심지어 빠르다.
   Rational Purify를 사용해 보았다면 이게 얼마나 중요한 문제인지 알 수 있는데 ...


3. 설치.


   그냥 Download 에서 사용할 플랫폼에 맞춰 다운로드 후 압축만 풀면 된다.
   적어도 리눅스에선 그렇다.
  


4. 예제 코드.


    굉장히 단순화 되어 있지만 의외로 복잡한 코드에 섞여 있을 때 찾기가 어려웠던 문제.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <map>
 
struct DS {
    int DS_1;
    int DS_2;
};
 
class Base {
    public:
        Base(void) {}
        ~Base(void) {}
};
 
class Derived {
    public:
        Derived(void) {}
        ~Derived(void) {}
        
        std::map<int, DS> m_map;
};
 
int main(int argc, char* args[])
{
    Derived* pD = new Derived();
    pD -> m_map[0= DS();
    Base* pB = static_cast<Base*> (pD);
    delete pB;
}
cs

1
g++ -g ./memoryleaktest.cpp
cs

    그리고 drmemory -- ./a.out 형태로 테스트 가능하다.



5. 결론.


    소멸자를 가상 함수로 만들지 않아 Derived::~Derived 소멸자가 호출되지 않는 문제를
    정확하게 진단해 준다.

6. 그 밖의 옵션들.


    보통 서버는 왠만하면 계속 그냥 떠 있게 된다. 그래서 중간 중간 확인할 수 있는
    방법도 이미 만들어져 있다.

1
drmemory -nudge <PID>
cs

    유용한 옵션으로 로그 저장 경로를 변경 가능하다
1
drmemory -logdir /var/log/DrMemoryLog/ -- ./a.out
cs